WsProvider

A JSON-RPC provider that connects to a WebSocket endpoint

const provider = new WsProvider('wss://rpc.polkadot.io');

await provider.connect();

// Fetch the genesis hash
const genesisHash = await provider.send('chain_getBlockHash', [0]);
console.log(genesisHash);

// Subscribe to runtimeVersion changes
await provider.subscribe(
{
subname: 'chain_newHead',
subscribe: 'chain_subscribeNewHeads',
params: [],
unsubscribe: 'chain_unsubscribeNewHeads',
},
(error, newHead, subscription) => {
console.log('newHead', newHead);
},
);

await provider.disconnect();

Hierarchy

  • SubscriptionProvider
    • WsProvider

Constructors

Properties

_handlers: Record<number, RequestState<any>>
_pendingNotifications: Record<string, JsonRpcResponseNotification<any>[]>
_subscriptions: Record<string, SubscriptionState>

Accessors

Methods

  • Returns void

  • Parameters

    Returns boolean

  • Parameters

    Returns this

  • Parameters

    Returns Unsub

  • Parameters

    Returns Unsub

  • Send a JSON-RPC request, make sure to connect to the provider first before sending requests

    Type Parameters

    • T = any

    Parameters

    • method: string
    • params: any[]

    Returns Promise<T>