@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
WSProviderInterface
Constructors
constructor
⊕ new WsProvider(endpoint?: string, autoConnect?: boolean): WsProvider
Defined in ws/index.ts:75
Parameters:
| Name | Type | Default value | Description |
|---|---|---|---|
Default value endpoint |
string |
defaults.WS_URL | The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944` |
Default value autoConnect |
boolean |
true | Whether to connect automatically or not. |
Returns: WsProvider
Methods
connect
▸ connect(): void
Defined in ws/index.ts:103
summary: Manually connect
description: The WsProvider connects automatically by default, however if you decided otherwise, you may connect manually using this method.
Returns: void
isConnected
▸ isConnected(): boolean
Defined in ws/index.ts:120
summary: Whether the node is connected or not.
Returns: boolean
true if connected
on
▸ on(type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void
Defined in ws/index.ts:129
summary: Listens on events after having subscribed using the subscribe function.
Parameters:
| Name | Type | Description |
|---|---|---|
| type | ProviderInterface$Emitted |
Event |
| sub | ProviderInterface$EmitCb |
Callback |
Returns: void
send
▸ send(method: string, params: Array<any>, subscription?: SubscriptionHandler): Promise<any>
Defined in ws/index.ts:139
summary: Send JSON data using WebSockets to configured HTTP Endpoint or queue.
Parameters:
| Name | Type | Description |
|---|---|---|
| method | string |
The RPC methods to execute |
| params | Array<any> |
Encoded paramaters as appliucable for the method |
Optional subscription |
SubscriptionHandler |
Subscription details (internally used) |
Returns: Promise<any>
subscribe
▸ subscribe(type: string, method: string, params: Array<any>, callback: ProviderInterface$Callback): Promise<number>
Defined in ws/index.ts:195
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.balances.freeBalance, <Address>]], (_, values) => {
console.log(values)
}).then((subscriptionId) => {
console.log('balance changes subscription id: ', subscriptionId)
})
Parameters:
| Name | 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:204
summary: Allows unsubscribing to subscriptions made with subscribe.
Parameters:
| Name | Type |
|---|---|
| type | string |
| method | string |
| id | number |
Returns: Promise<boolean>