Skip to main content
Version: 3.6.0

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.');
});

Error Handling​

The SDK includes error handling to ensure that issues are communicated effectively.

Emitting Errors​

Errors are emitted via the error event. You can listen to this event to handle errors gracefully.

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

Common Error Scenarios​

  • Missing Configuration: Errors will be emitted if required configuration options like websocketUrl or authenticationUrl are missing.
  • Authentication Failure: If authentication fails, an error with the corresponding message will be emitted.
  • WebSocket Issues: Errors related to WebSocket connections, such as connection failures or unexpected closures, will be emitted.

Error Object Structure​

When an error is emitted, it follows the following structure:

{
category: 'WEBSOCKET', // Error category (available types: AUTHENTICATION, OCO, VALIDATION, WEBSOCKET)
code: 'INVALID_WEBSOCKET_URL', // Error code that identifies which error just happened
message: 'Empty or invalid Websocket URL', // Error message explaining the problem
details: null // Error details which may or not bring additional information
suggestions: [] // List of suggestions on how to fix the error
}