Skip to content

WebSocket Connections

martiliones edited this page Dec 17, 2023 · 1 revision

To use websocket, you have to create a websocket client and connect it to the API instance:

// socket.js
import {WebSocketClient} from 'adamant-api';

const ws = new WebSocketClient({
  /* ... */
});

export {ws};
// api.js
import {AdamantApi} from 'adamant-api';
import {ws} from './socket.js';

const api = new AdamantApi({
  /* ... */
});

api.initSocket(ws); // <-- WebSocket initialization

export {api};

Options

type WsType = 'ws' | 'wss';

interface WsOptions {
  /**
   * ADM address to subscribe to notifications
   */
  admAddress: AdamantAddress;

  /**
   * Websocket type: `'wss'` or `'ws'`. Default is `'ws'`
   */
  wsType?: WsType;

  /**
   * Must connect to node with minimum ping. Not recommended. Default is `false`.
   */
  useFastest?: boolean;

  logger?: Logger;
}

For example:

const ws = new WebSocketClient({admAddress: 'U1234567890', useFastest: true});

.catch()

Sets an error handler for all event handlers.

  • Typing

    function catch(callback: ErrorHandler): this;
  • Example

    socket.onMessage(() => throw new Error('catch me'));
    
    socket.catch(error => {
      console.log(error); // Error: catch me
    });

.off()

Removes the handler from all types.

  • Typing

    function off(handler: SingleTransactionHandler): this;
  • Example

    The myFunction function will be called only once:

    function myFunction() {
      console.log('Hello!');
      socket.off(this);
    }
    
    socket.on(myFunction);

.on()

Adds an event listener handler for all transaction types or specific transaction types.

  • Typing

    function on(handler: AnyTransactionHandler): this;
    function on<T extends EventType>(
      types: T | T[],
      handler: TransactionHandler<TransactionMap[T]>
    ): this;
  • Parameters

    • handler — Function to handle transactions.
    • types — Specific transaction type(s).
  • Returns

    The current instance (this).

  • Example

    Generic Handler:

    api.on(genericTransactionHandler);

    Specific Transaction Type:

    api.on(TransactionType.SEND, sendTransactionHandler);

.onMessage()

Registers an event handler for Chat Message transactions.

  • Typing

    function onMessage(handler: TransactionHandler<ChatMessageTransaction>): this;
  • Parameters

    • handler — Function to handle Chat Message transactions.
  • Returns

    The current instance (this).

  • Example

    api.onMessage(chatMessageHandler);

.onTransfer()

Registers an event handler for Token Transfer transactions.

  • Typing

    function onTransfer(
      handler: TransactionHandler<TokenTransferTransaction>
    ): this;
  • Parameters

    • handler — Function to handle Token Transfer transactions.
  • Returns

    The current instance (this).

  • Example

    api.onTransfer(tokenTransferHandler);

.onNewDelegate()

Registers an event handler for Register Delegate transactions.

  • Typing

    function onNewDelegate(
      handler: TransactionHandler<RegisterDelegateTransaction>
    ): this;
  • Parameters

    • handler — Function to handle Register Delegate transactions.
  • Returns

    The current instance (this).

  • Example

    api.onNewDelegate(registerDelegateHandler);

.onVoteForDelegate()

Registers an event handler for Vote for Delegate transactions.

  • Typing

    function onVoteForDelegate(
      handler: TransactionHandler<VoteForDelegateTransaction>
    ): this;
  • Parameters

    • handler — Function to handle Vote for Delegate transactions.
  • Returns

    The current instance (this).

  • Example

    api.onVoteForDelegate(voteForDelegateHandler);

.onKVS()

Registers an event handler for Key-Value Store (KVS) transactions.

  • Typing

    function onKVS(handler: TransactionHandler<KVSTransaction>): this;
  • Parameters

    • handler — Function to handle KVS transactions.
  • Returns

    The current instance (this).

  • Example

    api.onKVS(kvsHandler);