-
-
Notifications
You must be signed in to change notification settings - Fork 8
WebSocket Connections
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};
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});
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 });
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);
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);
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);
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);
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);
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);
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);