/ SDKs / JavaScript
SDKs
Chat SDKs JavaScript v4
Chat SDKs JavaScript
Chat SDKs
JavaScript
Version 4

Add or remove a connection event handler

Copy link

To detect changes in the connection status of a client app, add a connection event handler with its UNIQUE_HANDLER_ID by calling sb.addConnectionHandler().

If you want to stay informed of changes related to the the Sendbird server connection status and notify the changes to different client apps, define and register multiple connection event handlers using the UNIQUE_HANDLER_ID.


Connection event types

Copy link

List of connection events

Copy link
MethodInvoked whenNotified devices

onConnected()

SDK has connected to the Sendbird server successfully.

The device that successfully connected to the server and started the session.

onDisconnected()

SDK has disconnected and lost the session.

The device explicitly disconnected through sb.disconnect().

onReconnectStarted()

SDK has started reconnecting to the Sendbird server.

The device where sb.reconnect() was automatically called by the Chat SDK or manually by the client app.

onReconnectSucceeded()

SDK has succeeded in reconnecting to the Sendbird server.

The device that successfully reconnected to the server.

onReconnectFailed()

SDK has failed to reconnect to the Sendbird server.

The device that failed to reconnect to the server.


Add a connection event handler

Copy link

The following code shows a full set of supported event callbacks and how to add a connection event handler to the unique SendbirdChat instance.

JavaScriptTypeScript
const connectionHandler = new ConnectionHandler({
  onConnected: () => {},
  onDisconnected: () => {},
  onReconnectStarted: () => {},
  onReconnectSucceeded: () => {},
  onReconnectFailed: () => {},
});

sb.addConnectionHandler(UNIQUE_HANDLER_ID, connectionHandler);

Remove a connection event handler

Copy link

The following code shows how to remove the connection event handler.

JavaScriptTypeScript
sb.removeConnectionHandler(UNIQUE_HANDLER_ID);