Class WsProvider

@polkadot/rpc-provider/ws

Name

WsProvider

Description

The WebSocket Provider allows sending requests using WebSocket to a WebSocket RPC server TCP port. Unlike the HttpProvider, it does support subscriptions and allows listening to events such as new blocks or balance changes.

Example


import Api from '@polkadot/api/promise';
import { WsProvider } from '@polkadot/rpc-provider/ws';

const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Api(provider);

See

HttpProvider

Hierarchy

  • WsProvider

Implements

  • ProviderInterface

Constructors

  • Parameters

    • Optional endpoint: string | string[]

      The endpoint url. Usually ws://ip:9944 or wss://ip:9944, may provide an array of endpoint strings.

    • Optional autoConnectMs: number | false
    • Optional headers: Record<string, string>
    • Optional timeout: number

      Custom timeout value

    Returns WsProvider

Properties

#private: any

Accessors

  • get hasSubscriptions(): boolean
  • Summary

    true when this provider supports subscriptions

    Returns boolean

  • get isConnected(): boolean
  • Summary

    Whether the node is connected or not.

    Returns

    true if connected

    Returns boolean

  • get isReady(): Promise<WsProvider>
  • Description

    Promise that resolves the first time we are connected and loaded

    Returns Promise<WsProvider>

  • get stats(): ProviderStats
  • Description

    Returns the connection stats

    Returns ProviderStats

Methods

  • Description

    Returns a clone of the object

    Returns WsProvider

  • Summary

    Manually connect

    Description

    The WsProvider connects automatically by default, however if you decided otherwise, you may connect manually using this method.

    Returns Promise<void>

  • Description

    Connect, never throwing an error, but rather forcing a retry

    Returns Promise<void>

  • Description

    Manually disconnect from the connection, clearing auto-connect logic

    Returns Promise<void>

  • Summary

    Listens on events after having subscribed using the subscribe function.

    Returns

    unsubscribe function

    Parameters

    • type: ProviderInterfaceEmitted

      Event

    • sub: ProviderInterfaceEmitCb

      Callback

    Returns (() => void)

      • (): void
      • Summary

        Listens on events after having subscribed using the subscribe function.

        Returns

        unsubscribe function

        Returns void

  • Summary

    Send JSON data using WebSockets to configured HTTP Endpoint or queue.

    Type Parameters

    • T = any

    Parameters

    • method: string

      The RPC methods to execute

    • params: unknown[]

      Encoded parameters as applicable for the method

    • Optional isCacheable: boolean
    • Optional subscription: SubscriptionHandler

      Subscription details (internally used)

    Returns Promise<T>

  • Name

    subscribe

    Summary

    Allows subscribing to a specific event.

    Example


    const provider = new WsProvider('ws://127.0.0.1:9944');
    const rpc = new Rpc(provider);

    rpc.state.subscribeStorage([[storage.system.account, <Address>]], (_, values) => {
    console.log(values)
    }).then((subscriptionId) => {
    console.log('balance changes subscription id: ', subscriptionId)
    })

    Parameters

    • type: string
    • method: string
    • params: unknown[]
    • callback: ProviderInterfaceCallback

    Returns Promise<string | number>

  • Summary

    Allows unsubscribing to subscriptions made with subscribe.

    Parameters

    • type: string
    • method: string
    • id: string | number

    Returns Promise<boolean>

Generated using TypeDoc