# Sendibird exception: WebSocket connection must be made first

**URL:** <https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692>\
**Category:** JavaScript\
**Tags:** chatsdk, react-native\
**Created:** [May 19, 2022, 1:52pm UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692 "2022-05-19T13:52:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Otavio\_Barbosa](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/otavio_barbosa/32/2462_2.png) [@Otavio\_Barbosa](https://community.sendbird.com/u/Otavio_Barbosa)\
**Post date:** [May 19, 2022, 1:52pm UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692/1 "2022-05-19T13:52:08Z")

</div>

Hi, I’m getting so many occurrences of sendbird exception in sentry, most of them related to WebSocket (see image below).

Any ideas of what is causing this issue?

Sendbird version: 3.1.9  
React Native version: 0.68.1  
React: 18.1.0

 ![sendbird](https://us1.discourse-cdn.com/flex020/uploads/sendbird/original/2X/7/7cf159889b801467a26c187803fb8ba13cc17bd4.png)

---

<div class="post-metadata">

**Author:** ![Yongjun\_Choi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/yongjun_choi/32/36_2.png) [@Yongjun\_Choi](https://community.sendbird.com/u/Yongjun_Choi)\
**Post date:** [May 20, 2022, 3:23am UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692/2 "2022-05-20T03:23:55Z")

</div>

Hi @Otavio_Barbosa  
you must be connected to the Sendbird server before calling the Sendbird API.  
please check which API is called before the error code,  
and connect before calling the API thanks.

---

<div class="post-metadata">

**Author:** ![aleksandr.bolshakov](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@aleksandr.bolshakov](https://community.sendbird.com/u/aleksandr.bolshakov)\
**Post date:** [August 31, 2022, 12:17pm UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692/3 "2022-08-31T12:17:25Z")

</div>

@Yongjun_Choi  
I got this issue on changing AppState from active to inactive (background /closed app)  
on change app state from ‘active’ we are calling:

```auto
      sendbird.removeAllChannelHandlers();
      sendbird.removeAllConnectionHandlers();
      sendbird.setBackgroundState();

```

but getting error:

```auto
[ERROR] [SendBirdException: WebSocket connection must be made first.]

```

---

<div class="post-metadata">

**Author:** ![Yongjun\_Choi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/yongjun_choi/32/36_2.png) [@Yongjun\_Choi](https://community.sendbird.com/u/Yongjun_Choi)\
**Post date:** [August 31, 2022, 12:45pm UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692/4 "2022-08-31T12:45:24Z")

</div>

hi @aleksandr.bolshakov  
sendbird.setBackgroundState();

If the status changes to the background state, the connection with Sendbird will be lost. Before changing to setForegroundState(), can you check if Sendbird API is called?

---

<div class="post-metadata">

**Author:** ![aleksandr.bolshakov](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@aleksandr.bolshakov](https://community.sendbird.com/u/aleksandr.bolshakov)\
**Post date:** [August 31, 2022, 12:58pm UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692/5 "2022-08-31T12:58:39Z")

</div>

Hi, @Yongjun_Choi !  
What do you mean?  
Check is I calling `setForegroundState()`

---

<div class="post-metadata">

**Author:** ![Yongjun\_Choi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/yongjun_choi/32/36_2.png) [@Yongjun\_Choi](https://community.sendbird.com/u/Yongjun_Choi)\
**Post date:** [August 31, 2022, 1:01pm UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692/6 "2022-08-31T13:01:53Z")

</div>

setBackgroundState(); - will disconnect  
**=\> Is there any Sendbird api call before the connection? (here)**  
setForegroundState(); - try reconnect

---

<div class="post-metadata">

**Author:** ![aleksandr.bolshakov](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@aleksandr.bolshakov](https://community.sendbird.com/u/aleksandr.bolshakov)\
**Post date:** [September 20, 2022, 7:33pm UTC](https://community.sendbird.com/t/sendibird-exception-websocket-connection-must-be-made-first/4692/7 "2022-09-20T19:33:40Z")

</div>

@Yongjun_Choi We have handler like this:

```auto
  useEffect(() => {
    AppState.addEventListener('change', handleStateChange);

    return () => {
      sendbird.removeAllChannelHandlers();
      sendbird.removeAllConnectionHandlers();
      AppState.removeEventListener('change', handleStateChange);
    };
  }, []);

  const handleStateChange = (newState: string) => {
    if (newState === 'active') {
      sendbird.setForegroundState();
    } else {
      sendbird.setBackgroundState();
    }
  };

```
