Pezkuwi API
    Preparing search index...

    Class WsProvider

    @pezkuwi/rpc-provider/ws

    WsProvider

    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.


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

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

    [[HttpProvider]]

    Implements

    • ProviderInterface
    Index
    • Parameters

      • endpoint: string | string[] = defaults.WS_URL

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

      • autoConnectMs: number | false = RETRY_DELAY

        Whether to connect automatically or not (default). Provided value is used as a delay between retries.

      • headers: Record<string, string> = {}

        The headers provided to the underlying WebSocket

      • Optionaltimeout: number

        Custom timeout value used per request . Defaults to DEFAULT_TIMEOUT_MS

      • OptionalcacheCapacity: number

        Custom size of the WsProvider LRUCache. Defaults to DEFAULT_CAPACITY (1024)

      • OptionalcacheTtl: number | null

        Custom TTL of the WsProvider LRUCache. Determines how long an object can live in the cache. Defaults to DEFAULT_TTL` (30000)

      Returns WsProvider

    • Returns Promise<void>

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

    • Returns Promise<void>

      Manually disconnect from the connection, clearing auto-connect logic

    • Parameters

      • type: ProviderInterfaceEmitted

        Event

      • sub: ProviderInterfaceEmitCb

        Callback

      Returns () => void

      unsubscribe function

    • Type Parameters

      • T = any

      Parameters

      • method: string

        The RPC methods to execute

      • params: unknown[]

        Encoded parameters as applicable for the method

      • OptionalisCacheable: boolean
      • Optionalsubscription: SubscriptionHandler

        Subscription details (internally used)

      Returns Promise<T>

    • Parameters

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

      Returns Promise<string | number>

      subscribe


      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
      • id: string | number

      Returns Promise<boolean>