Options
All
  • Public
  • Public/Protected
  • All
Menu

Class WebSocketClient

An object repesenting a connection to a Terra node's WebSocket RPC endpoint. This allows for subscribing to Tendermint events through WebSocket.

Events

  • error emitted when error raises
  • connect emitted after connection establishment
  • reconnect emitted upon every attempt of reconnection
  • destroyed emitted when socket has been destroyed

Example

import { TendermintQuery, WebSocketClient } from '@terra-money/terra.js';

const wsclient = new WebSocketClient("ws://localhost:26657/websocket");
wsclient.start();
wsclient.on('connect', () => {
wsclient.subscribe('NewBlock', new TendermintQuery(), (data) => {
console.log(data.value);

// close after receiving one block.
wsclient.destroy();
});

wsclient.subscribe(
'Tx',
new TendermintQuery()
.exact('message.action', 'send')
.contains('message.sender', 'terra1...'),
(data) => {
console.log(data.value);

// close after receiving one send Tx
wsclient.destroy();
},
);
});

Hierarchy

  • EventEmitter
    • WebSocketClient

Index

Constructors

  • new WebSocketClient(URL: string, reconnectCount?: number, reconnectInterval?: number): WebSocketClient
  • WebSocketClient constructor

    Parameters

    • URL: string

      The WebSocket endpoint URL on the Tendermint RPC server. Ex: ws://localhost:26657/websocket

    • reconnectCount: number = 0

      0 for not to attempt reconnect, -1 for infinite, > 0 for number of times to attempt

    • reconnectInterval: number = 1000

      retry interval in milliseconds

    Returns WebSocketClient

Accessors

  • get isConnected(): boolean

Methods

  • destroy(): void
  • start(): void