Instantiating the Client

LAST EDIT Oct 14 2024

In every client-side chat integration, you will need to open a WebSocket connection between the client device (cell phone/laptop/tablet, etc.) and the API using the connectUser(); (or SDK equivalent) method. Let's look at what the connectUser method is doing.

This method performs multiple functions:

  • The primary purpose of this method is to open a WebSocket connection. Thus, it should only be used once per session per user. Opening multiple WebSockets per user opens your application to a myriad of problems, including performance, billing, and unexpected behavior.

  • It Gets or Creates a new user. It will increase your MAU (monthly active users), Concurrent Connections (as long as the WebSocket is active), and is not a valid method for importing users.

  • It partially updates the user. Any key:value data fields passed in addition to the user_id are added or updated in the case that they already exist.

  • The method returns the unread counts for the user.

  • It should never be used server-side. Some SDK’s that can be used both server-side and client-side (JS SDK) expose this method in both places, so this is possible and strongly discouraged.

  • The WebSocket can be closed using chatClient.disconnectUser(); (or language SDK equivalent).

  • The API response from this method contains useful information (more of this later).

In order to prevent opening an excessive number of Websocket connections per user, stream will limit the number of open connection to 50 in production, and 3 in development mode.

Examples

Copied!

When using connectUser make sure the method will not execute more frequently than necessary and that it disconnects the user when the chat component dismounts:

Here's an example of how an integration could overuse the connectUser function, creating far more concurrent connections than is required to accomplish the desired behavior. (This can result in driving up your bill, stability issues, or even just unexpected behavior when interacting with multiple open WebSockets):

Debugging Multiple WebSocket Connections

Copied!

When testing, make sure that only a single WebSocket connection is open at a given time. We recommend refreshing webpages, navigating to and from the chat page, and testing all features of the chat user experience with the Chrome (or other browser) Network tab open.

The Chrome Network tools has a WS (WebSocket) tab that can be used to view the WebSocket connections. There should only be one. Below is a screen capture of an application with multiple open, which indicates an integration problem.

The below screenshot is of a correct implementation, where WebSockets are closed prior to another being opened.