Skip to main content
Version: 3.6.0

Establishing a WebSocket Connection

After authentication (if required), you can establish a WebSocket connection using the obtained token.

Usage​

async function connectWebSocket() {
const token = await client.authenticate();
client.openConnection(token);
}

Event Handling​

The SDK emits several events to handle various states and messages. You can listen to these events to manage your application's behavior accordingly.

Available Events​

  • open: Emitted when the WebSocket connection is successfully opened.
  • message: Emitted when a message is received from the server.
  • error: Emitted when an error occurs.
  • close: Emitted when the WebSocket connection is closed.

Listening to Events​

client.on('open', () => {
console.log('WebSocket connection opened.');
});

client.on('message', (payload) => {
const message = JSON.parse(payload);
console.log('Received message:', message);
});

client.on('error', (error) => {
console.error('An error occurred:', error);
});

client.on('close', () => {
console.log('WebSocket connection closed.');
});