UNPKG

3.43 kBTypeScriptView Raw
1import { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
2import './polyfill';
3import E3 from 'eventemitter3';
4declare type SubscriptionHandler = {
5 callback: ProviderInterface$Callback;
6 type: string;
7};
8interface WSProviderInterface extends ProviderInterface {
9 connect(): void;
10}
11/**
12 * The WebSocket Provider allows sending requests using WebSocket. Unlike the [[HttpProvider]],
13 * it does support subscriptions and allows listening to events such as new blocks or balance changes.
14 *
15 * @example
16 * import createApi from '@polkadot/api';
17 * import WsProvider from '@polkadot/api-provider/ws';
18 * const provider = new WsProvider('ws://127.0.0.1:9944');
19 * const api = createApi(provider);
20 *
21 * @see [[HttpProvider]]
22 */
23export default class WsProvider extends E3.EventEmitter implements WSProviderInterface {
24 private autoConnect;
25 private coder;
26 private endpoint;
27 private handlers;
28 private _isConnected;
29 private l;
30 private queued;
31 private subscriptions;
32 private websocket;
33 /**
34 * @param {string} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`
35 * @param {boolean = true} autoConnect Whether to connect automatically or not.
36 */
37 constructor(endpoint: string, autoConnect?: boolean);
38 /**
39 * The [[WsProvider]] connects automatically by default. if you decided otherwise, you may
40 * connect manually using this method.
41 */
42 connect(): void;
43 /**
44 * Whether the node is connected or not.
45 * @return {boolean} true if connected
46 */
47 isConnected(): boolean;
48 /**
49 * Listens on events after having subscribed using the [[subscribe]] function.
50 * @param {ProviderInterface$Emitted} type Event
51 * @param {ProviderInterface$EmitCb} sub Callback
52 * @return {this} [description]
53 */
54 on(type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): this;
55 send(method: string, params: Array<any>, subscription?: SubscriptionHandler): Promise<any>;
56 /**
57 * Allows subscribing to a specific event.
58 * @param {string} type Subscription type
59 * @param {string} method Subscription method
60 * @param {Array<any>} params Parameters
61 * @param {ProviderInterface$Callback} callback Callback
62 * @return {Promise<number>} Promise resolving to the dd of the subscription you can use with [[unsubscribe]].
63 *
64 * @example
65 * const provider = new WsProvider('ws://127.0.0.1:9944');
66 * const api = createApi(provider);
67 *
68 * api.state.storage([[storage.balances.freeBalance, <Address>]], (_, values) => {
69 * console.log(values)
70 * }).then((subscriptionId) => {
71 * console.log('balance changes subscription id: ', subscriptionId)
72 * })
73 */
74 subscribe(type: string, method: string, params: Array<any>, callback: ProviderInterface$Callback): Promise<number>;
75 /**
76 * Allows unsubscribing to subscriptions made with [[subscribe]].
77 */
78 unsubscribe(type: string, method: string, id: number): Promise<boolean>;
79 private onSocketClose;
80 private onSocketError;
81 private onSocketMessage;
82 private onSocketMessageResult;
83 private onSocketMessageSubscribe;
84 private onSocketOpen;
85}
86export {};