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

  • Parameters

    Returns WsProvider

Properties

_handlers: Record<number, RequestState<any>>
_onReceiveResponse: ((response: string) => void)
_pendingNotifications: Record<string, JsonRpcResponseNotification<any>[]>
_subscriptions: Record<string, SubscriptionState>

Accessors

  • get status(): ConnectionStatus
  • The current connection status

    Returns ConnectionStatus

Methods

  • Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Returns void

  • Returns Promise<WsProvider>

  • Returns Promise<void>

  • Parameters

    Returns Promise<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>