393 lines
9.5 KiB
Markdown
393 lines
9.5 KiB
Markdown
|
|
|
|
*__name__*: WsProvider
|
|
|
|
*__summary__*: The WebSocket Provider allows sending requests using WebSocket to a WebSocket RPC server TCP port.
|
|
|
|
*__description__*: Unlike the [HttpProvider](_http_index_.httpprovider.md), it does support subscriptions and allows listening to events such as new blocks or balance changes.
|
|
|
|
*__example__*:
|
|
```javascript
|
|
import Api from '@polkadot/api';
|
|
import WsProvider from '@polkadot/api-provider/ws';
|
|
|
|
const provider = new WsProvider('ws://127.0.0.1:9944');
|
|
const api = new Api(provider);
|
|
```
|
|
|
|
*__see__*: [HttpProvider](_http_index_.httpprovider.md)
|
|
|
|
# Type parameters
|
|
#### EventTypes : `string` | `symbol`
|
|
|
|
# Hierarchy
|
|
|
|
`EventEmitter`
|
|
|
|
**↳ WsProvider**
|
|
|
|
# Implements
|
|
|
|
* `WSProviderInterface`
|
|
|
|
# Constructors
|
|
|
|
<a id="constructor"></a>
|
|
|
|
## constructor
|
|
|
|
⊕ **new WsProvider**(endpoint: *`string`*, autoConnect?: *`boolean`*): [WsProvider](_ws_index_.wsprovider.md)
|
|
|
|
*Defined in [ws/index.ts:73](https://github.com/polkadot-js/api/blob/dc88797/packages/api-provider/src/ws/index.ts#L73)*
|
|
|
|
**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](_ws_index_.wsprovider.md)
|
|
|
|
___
|
|
|
|
# Properties
|
|
|
|
<a id="prefixed"></a>
|
|
|
|
## `<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
|
|
|
|
<a id="addlistener"></a>
|
|
|
|
## 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`
|
|
|
|
___
|
|
<a id="connect"></a>
|
|
|
|
## connect
|
|
|
|
▸ **connect**(): `void`
|
|
|
|
*Defined in [ws/index.ts:104](https://github.com/polkadot-js/api/blob/dc88797/packages/api-provider/src/ws/index.ts#L104)*
|
|
|
|
*__summary__*: Manually connect
|
|
|
|
*__description__*: The [WsProvider](_ws_index_.wsprovider.md) connects automatically by default, however if you decided otherwise, you may connect manually using this method.
|
|
|
|
**Returns:** `void`
|
|
|
|
___
|
|
<a id="emit"></a>
|
|
|
|
## 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`
|
|
|
|
___
|
|
<a id="eventnames"></a>
|
|
|
|
## 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`>
|
|
|
|
___
|
|
<a id="isconnected"></a>
|
|
|
|
## isConnected
|
|
|
|
▸ **isConnected**(): `boolean`
|
|
|
|
*Defined in [ws/index.ts:121](https://github.com/polkadot-js/api/blob/dc88797/packages/api-provider/src/ws/index.ts#L121)*
|
|
|
|
*__summary__*: Whether the node is connected or not.
|
|
|
|
**Returns:** `boolean`
|
|
true if connected
|
|
|
|
___
|
|
<a id="listenercount"></a>
|
|
|
|
## 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`
|
|
|
|
___
|
|
<a id="listeners"></a>
|
|
|
|
## 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`>
|
|
|
|
___
|
|
<a id="off"></a>
|
|
|
|
## off
|
|
|
|
▸ **off**(event: *`EventTypes`*, fn?: *[ListenerFn](../interfaces/_ws_index_.wsprovider.eventemitter.listenerfn.md)*, 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](../interfaces/_ws_index_.wsprovider.eventemitter.listenerfn.md) |
|
|
| `Optional` context | `any` |
|
|
| `Optional` once | `undefined` | `false` | `true`|
|
|
|
|
**Returns:** `this`
|
|
|
|
___
|
|
<a id="on"></a>
|
|
|
|
## on
|
|
|
|
▸ **on**(type: *[ProviderInterface$Emitted](../modules/_types_d_.md#providerinterface_emitted)*, sub: *[ProviderInterface$EmitCb](../modules/_types_d_.md#providerinterface_emitcb)*): `this`
|
|
|
|
*Overrides EventEmitter.on*
|
|
|
|
*Defined in [ws/index.ts:131](https://github.com/polkadot-js/api/blob/dc88797/packages/api-provider/src/ws/index.ts#L131)*
|
|
|
|
*__summary__*: Listens on events after having subscribed using the [subscribe](_ws_index_.wsprovider.md#subscribe) function.
|
|
|
|
**Parameters:**
|
|
|
|
| Param | Type | Description |
|
|
| ------ | ------ | ------ |
|
|
| type | [ProviderInterface$Emitted](../modules/_types_d_.md#providerinterface_emitted) | Event |
|
|
| sub | [ProviderInterface$EmitCb](../modules/_types_d_.md#providerinterface_emitcb) | Callback |
|
|
|
|
**Returns:** `this`
|
|
[description]
|
|
|
|
___
|
|
<a id="once"></a>
|
|
|
|
## 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`
|
|
|
|
___
|
|
<a id="removealllisteners"></a>
|
|
|
|
## 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`
|
|
|
|
___
|
|
<a id="removelistener"></a>
|
|
|
|
## removeListener
|
|
|
|
▸ **removeListener**(event: *`EventTypes`*, fn?: *[ListenerFn](../interfaces/_ws_index_.wsprovider.eventemitter.listenerfn.md)*, 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](../interfaces/_ws_index_.wsprovider.eventemitter.listenerfn.md) |
|
|
| `Optional` context | `any` |
|
|
| `Optional` once | `undefined` | `false` | `true`|
|
|
|
|
**Returns:** `this`
|
|
|
|
___
|
|
<a id="send"></a>
|
|
|
|
## send
|
|
|
|
▸ **send**(method: *`string`*, params: *`Array`<`any`>*, subscription?: *`SubscriptionHandler`*): `Promise`<`any`>
|
|
|
|
*Defined in [ws/index.ts:138](https://github.com/polkadot-js/api/blob/dc88797/packages/api-provider/src/ws/index.ts#L138)*
|
|
|
|
*__summary__*: Send JSON data using WebSockets to configured HTTP Endpoint or queue.
|
|
|
|
**Parameters:**
|
|
|
|
| Param | Type |
|
|
| ------ | ------ |
|
|
| method | `string` |
|
|
| params | `Array`<`any`> |
|
|
| `Optional` subscription | `SubscriptionHandler` |
|
|
|
|
**Returns:** `Promise`<`any`>
|
|
|
|
___
|
|
<a id="subscribe"></a>
|
|
|
|
## subscribe
|
|
|
|
▸ **subscribe**(type: *`string`*, method: *`string`*, params: *`Array`<`any`>*, callback: *[ProviderInterface$Callback](../modules/_types_d_.md#providerinterface_callback)*): `Promise`<`number`>
|
|
|
|
*Defined in [ws/index.ts:194](https://github.com/polkadot-js/api/blob/dc88797/packages/api-provider/src/ws/index.ts#L194)*
|
|
|
|
*__name__*: subscribe
|
|
|
|
*__summary__*: Allows subscribing to a specific event.
|
|
|
|
*__example__*:
|
|
```javascript
|
|
const provider = new WsProvider('ws://127.0.0.1:9944');
|
|
const api = new Api(provider);
|
|
|
|
api.state.storage([[storage.balances.freeBalance, <Address>]], (_, 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](../modules/_types_d_.md#providerinterface_callback) | Callback |
|
|
|
|
**Returns:** `Promise`<`number`>
|
|
Promise resolving to the dd of the subscription you can use with [[unsubscribe]].
|
|
|
|
___
|
|
<a id="unsubscribe"></a>
|
|
|
|
## unsubscribe
|
|
|
|
▸ **unsubscribe**(type: *`string`*, method: *`string`*, id: *`number`*): `Promise`<`boolean`>
|
|
|
|
*Defined in [ws/index.ts:203](https://github.com/polkadot-js/api/blob/dc88797/packages/api-provider/src/ws/index.ts#L203)*
|
|
|
|
*__summary__*: Allows unsubscribing to subscriptions made with [subscribe](_ws_index_.wsprovider.md#subscribe).
|
|
|
|
**Parameters:**
|
|
|
|
| Param | Type |
|
|
| ------ | ------ |
|
|
| type | `string` |
|
|
| method | `string` |
|
|
| id | `number` |
|
|
|
|
**Returns:** `Promise`<`boolean`>
|
|
|
|
___
|
|
|