# Instantiating the Client

In every **client-side** chat integration, you will need to open a [WebSocket connection](/chat/docs/<framework>/init_and_users/) 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](https://github.com/GetStream/stream-chat-js)) 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).

<admonition type="info">

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.

</admonition>

### Examples

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:

<codetabs>

<codetabs-item value="javascript" label="JavaScript">

```js
// ✅ You should disconnect user once you are done with chatClient
const [chatClient, setChatClient] = useState(null);

useEffect(() => {
  const initChat = async () => {
    const client = StreamChat.getInstance("api_key");
    // open the WebSocket connection to start receiving events
    await client.connectUser({ id: "user_id" }, "user_token");
    setChatClient(client);
  };

  initChat();

  // close the WebSocket connection when component dismounts
  return () => client.disconnectUser();
}, []);

if (!chatClient) return null;

<Chat client={chatClient}>{/** children of Chat component*/}</Chat>;
```

</codetabs-item>

</codetabs>

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):

<codetabs>

<codetabs-item value="javascript" label="JavaScript">

```js
// ❌ This will create new websocket connections whenever
// useEffect hook gets executed (on changes to `currentUser`)

const [chatClient, setChatClient] = useState(null);
const [currentUser, setCurrentUser] = useState(null);

useEffect(() => {
  const initChat = async () => {
    const client = StreamChat.getInstance("api_key");
    const result = await client.connectUser({ id: "user_id" }, "user_token");
    setChatClient(client);
  };

  initChat();

  // No disconnect means useEffect will keep being called when user changes.
}, [currentUser]);
```

</codetabs-item>

</codetabs>

### Debugging Multiple WebSocket Connections

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.

![](https://i.imgur.com/QdUIgbV.png)

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

![](https://i.imgur.com/j4fiuDQ.png)

---

This page was last updated at 2025-08-15T14:14:39.339Z.

For the most recent version of this documentation, visit [https://getstream.io/chat/docs/ios-swift/instantiating_the_client/](https://getstream.io/chat/docs/ios-swift/instantiating_the_client/).