Choosing the Right Provider

Provider Types

The library offers two provider implementations, each designed for different communication patterns and application requirements. Both expose the same core API for querying blockchain state and broadcasting transactions, but differ in how they manage the underlying connection to the OP_NET node.

JSONRpcProvider

The JSONRpcProvider communicates over HTTP using the standard JSON-RPC protocol. Each method call sends an independent request and waits for a response, with no persistent connection maintained between calls. This stateless model makes it the default choice for most applications.

Best suited for:

  • Standard request/response operations.
  • Server-side applications and backend services where real-time updates are not required.
  • Stateless interactions.
  • Simple integrations.
typescript
Instanciating a JSONRpcProvider
import { JSONRpcProvider } from 'opnet';
import { networks } from '@btc-vision/bitcoin';

const provider = new JSONRpcProvider({
    url: 'https://regtest.opnet.org',
    network: networks.regtest
});

WebSocketRpcProvider (experimental)

The WebSocketRpcProvider maintains a persistent WebSocket connection to the OP_NET node, enabling the server to push updates to the client without polling. In addition to supporting all standard provider methods, it exposes subscription-based APIs for receiving real-time notifications as events occur on the network.

Best suited for:

  • Real-time block and epoch notifications without continuous polling.
  • Event-driven applications that need to react immediately to on-chain state changes.
  • Long-running processes that benefit from a persistent connection.
typescript
Instanciating a WebSocketRpcProvider
import { WebSocketRpcProvider } from 'opnet';
import { networks } from '@btc-vision/bitcoin';

const provider = new WebSocketRpcProvider({
    url: 'wss://regtest.opnet.org/ws',
    network: networks.regtest
);

// Subscribe to new blocks
provider.subscribeToBlocks((block) => {
    console.log('New block:', block.height);
});

Provider Selection

Use the following diagram to select the appropriate provider based on your application requirements:

How to Choose a Provider Need real-time updates? Need subscriptions? WebSocketRpcProvider JSONRpcProvider Block/Epoch notifications Standard operations |Yes| B: Need real-time? -> WebSocketRpcProvider --> Yes |No| C: Need real-time? -> Need subscriptions? --> No |Yes| B: Need subscriptions? -> WebSocketRpcProvider --> Yes |No| D: Need subscriptions? -> JSONRpcProvider --> No E: WebSocketRpcProvider -> Block/Epoch notifications --> F: JSONRpcProvider -> Standard operations -->