The WebSocket Provider allows sending requests using WebSocket. Unlike the HttpProvider, it does support subscriptions and allows listening to events such as new blocks or balance changes. example: import createApi from '@polkadot/api'; import WsProvider from '@polkadot/api-provider/ws'; const provider = new WsProvider('ws://127.0.0.1:9944'); const api = createApi(provider);

see: HttpProvider

Type parameters

EventTypes : string | symbol

Hierarchy

EventEmitter

↳ WsProvider

Implements

  • WSProviderInterface

Constructors

constructor

⊕ new WsProvider(endpoint: string, autoConnect?: boolean): WsProvider

Defined in ws/index.ts:67

Parameters:

Param Type Default value Description
endpoint string - The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`
Default value autoConnect boolean true Whether to connect automatically or not.

Returns: WsProvider


Properties

<Static> prefixed

● prefixed: string | boolean

Inherited from EventEmitter.prefixed

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:6


Methods

addListener

▸ addListener(event: EventTypes, fn: ListenerFn, context?: any): this

Inherited from EventEmitter.addListener

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:33

Parameters:

Param Type
event EventTypes
fn ListenerFn
Optional context any

Returns: this


connect

▸ connect(): void

Defined in ws/index.ts:97

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

Returns: void


emit

▸ emit(event: EventTypes, ...args: Array<any>): boolean

Inherited from EventEmitter.emit

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:27

Calls each of the listeners registered for a given event.

Parameters:

Param Type
event EventTypes
Rest args Array<any>

Returns: boolean


eventNames

▸ eventNames(): Array<EventTypes>

Inherited from EventEmitter.eventNames

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:12

Return an array listing the events for which the emitter has registered listeners.

Returns: Array<EventTypes>


isConnected

▸ isConnected(): boolean

Defined in ws/index.ts:114

Whether the node is connected or not.

Returns: boolean true if connected


listenerCount

▸ listenerCount(event: EventTypes): number

Inherited from EventEmitter.listenerCount

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:22

Return the number of listeners listening to a given event.

Parameters:

Param Type
event EventTypes

Returns: number


listeners

▸ listeners(event: EventTypes): Array<ListenerFn>

Inherited from EventEmitter.listeners

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:17

Return the listeners registered for a given event.

Parameters:

Param Type
event EventTypes

Returns: Array<ListenerFn>


off

▸ off(event: EventTypes, fn?: ListenerFn, context?: any, once?: undefined | false | true): this

Inherited from EventEmitter.off

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:44

Parameters:

Param Type
event EventTypes
Optional fn ListenerFn
Optional context any
Optional once undefined | false | true

Returns: this


on

▸ on(type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): this

Overrides EventEmitter.on

Defined in ws/index.ts:124

Listens on events after having subscribed using the subscribe function.

Parameters:

Param Type Description
type ProviderInterface$Emitted Event
sub ProviderInterface$EmitCb Callback

Returns: this [description]


once

▸ once(event: EventTypes, fn: ListenerFn, context?: any): this

Inherited from EventEmitter.once

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:38

Add a one-time listener for a given event.

Parameters:

Param Type
event EventTypes
fn ListenerFn
Optional context any

Returns: this


removeAllListeners

▸ removeAllListeners(event?: EventTypes): this

Inherited from EventEmitter.removeAllListeners

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:49

Remove all listeners, or those of the specified event.

Parameters:

Param Type
Optional event EventTypes

Returns: this


removeListener

▸ removeListener(event: EventTypes, fn?: ListenerFn, context?: any, once?: undefined | false | true): this

Inherited from EventEmitter.removeListener

Defined in /home/travis/build/polkadot-js/api/node_modules/eventemitter3/index.d.ts:43

Remove the listeners of a given event.

Parameters:

Param Type
event EventTypes
Optional fn ListenerFn
Optional context any
Optional once undefined | false | true

Returns: this


send

▸ send(method: string, params: Array<any>, subscription?: SubscriptionHandler): Promise<any>

Defined in ws/index.ts:128

Parameters:

Param Type
method string
params Array<any>
Optional subscription SubscriptionHandler

Returns: Promise<any>


subscribe

▸ subscribe(type: string, method: string, params: Array<any>, callback: ProviderInterface$Callback): Promise<number>

Defined in ws/index.ts:179

Allows subscribing to a specific event. example: const provider = new WsProvider('ws://127.0.0.1:9944'); const api = createApi(provider);

api.state.storage([[storage.balances.freeBalance,

]], (_, values) => { console.log(values) }).then((subscriptionId) => { console.log('balance changes subscription id: ', subscriptionId) })

Parameters:

Param Type Description
type string Subscription type
method string Subscription method
params Array<any> Parameters
callback ProviderInterface$Callback Callback

Returns: Promise<number> Promise resolving to the dd of the subscription you can use with [[unsubscribe]].


unsubscribe

▸ unsubscribe(type: string, method: string, id: number): Promise<boolean>

Defined in ws/index.ts:188

Allows unsubscribing to subscriptions made with subscribe.

Parameters:

Param Type
type string
method string
id number

Returns: Promise<boolean>


results matching ""

    No results matching ""