# Issue creating session immediately after leaving another session

**URL:** <https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058>\
**Category:** Web\
**Tags:** video-sdk\
**Created:** [May 16, 2024, 5:38am UTC](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058 "2024-05-16T05:38:54Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![vic.yang](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/vic.yang/32/24656_2.png) [@vic.yang](https://devforum.zoom.us/u/vic.yang)\
**Post date:** [May 27, 2024, 7:30am UTC](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058/21 "2024-05-27T07:30:29Z")

</div>

Hey @hazzal

Thanks for sharing the code snippet with us.

After analyzing the code, we found some issues when using the Video SDK that might cause the ‘IMPROPER\_MEETING\_STATE’ error. Here are some recommendations we provide:

> ```javascript
> const initialiseCall = useCallback(async () => {
> /**
> * Skip unrelated code.
> **/
> await client.init("en-US", "Global", {
> patchJsMedia: true,
> leaveOnPageUnload: true,
> });
> await client.join(zoomJwtResponse.session, zoomJwtResponse.jwt, username);
> console.log("session", client.getSessionInfo());
> /**
> * Skip unrelated code.
> **/
> // The next line mighe return rejected promise with "IMPROPER_MEETING_STATE"
> await zoomStream.startAudio();
> }, [
> client,
> consultationId,
> consultationService,
> isPatient,
> renderVideo,
> username,
> ]);
> // Automatically execute once the `initialiseCall` method changes
> useEffect(() => {
> void initialiseCall();
> }, [initialiseCall]);
> 
> // Another place that calls the init and join methods.
> const joinClient = useCallback(async () => {
> try {
> const remoteViewElement = canvasElementRef.current;
> const zoomJwtResponse = await consultationService.getZoomJwt(
> consultationId
> );
> console.log("joining zoom session with", zoomJwtResponse);
> await client.init("en-US", "Global", {
> patchJsMedia: true,
> leaveOnPageUnload: true,
> });
> await client.join(zoomJwtResponse.session, zoomJwtResponse.jwt, username);
> console.log("session", client.getSessionInfo());
> // The next line mighe return rejected promise with "IMPROPER_MEETING_STATE"
> await streamRef.current.startVideo();
> } catch (error) {
> console.error("Error joining Zoom session:", error);
> }
> });
> 
> ```

There are **two rejoin logics** in the code. According to my analysis and understanding, when you call the `createNewSession` and `joinClient` methods, the `initialiseCall` method within the `useEffect` will also execute automatically. The issue arises here. Since these two executions are independent, they are not blocked by `await`, leading to the following situation:

```auto

// execute sequence

-> await client.join // joinClient method

-> await client.init // initialiseCall method

```

When the `client.join` statement is executed in the `joinClient` method, the `initialiseCall` method might simultaneously execute the `client.init` statement. Since the `client.init` method resets the entire session state, the subsequent code in the `joinClient` method will not run as expected, which may result in the `IMPROPER_MEETING_STATE` error.

Either of the following solutions are viable:

- Refine the `initialiseCall` method so that it is only called when the component mounts.

- Remove the `joinClient` method and let React automatically trigger the `initialiseCall` method to join a new session when the `consultationId` changes. However, ensure that you refine the `initialiseCall` method by adding a cleanup function, as repeatedly binding event handlers to the `client.on` method might also cause issues.

Please let me know if you need any assistance with the issue.

Thanks  
Vic

---

<div class="post-metadata">

**Author:** ![hazzal](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/hazzal/32/59747_2.png) [@hazzal](https://devforum.zoom.us/u/hazzal)\
**Post date:** [May 28, 2024, 6:24pm UTC](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058/22 "2024-05-28T18:24:26Z")

</div>

Hey @vic.yang

Ill give that a go, but the join client I just added to put a button in for testing, It doesnt ever get run without me pressing a temporary button I added, just so I didnt have to mess around with mounting the component to test joining and leaving a session.

Regards Harry

---

<div class="post-metadata">

**Author:** ![vic.yang](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/vic.yang/32/24656_2.png) [@vic.yang](https://devforum.zoom.us/u/vic.yang)\
**Post date:** [May 29, 2024, 2:35am UTC](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058/23 "2024-05-29T02:35:28Z")

</div>

Hey @hazzal

Notably, since the `initialiseCall` method is scheduled to run by React, you need to pay attention to the dependencies of the `useCallback` within `initialiseCall`. Any changes in these dependencies will cause `initialiseCall` to be reassigned, which will trigger its execution. You can add breakpoints or logs to inspect this.

Thanks  
Vic

---

<div class="post-metadata">

**Author:** ![hazzal](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/hazzal/32/59747_2.png) [@hazzal](https://devforum.zoom.us/u/hazzal)\
**Post date:** [June 4, 2024, 1:17am UTC](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058/24 "2024-06-04T01:17:56Z")

</div>

Hey @vic.yang

The issue was that the dependencies were changing and causing the client to re-initialize during other functions running.

Thanks for the help

---

<div class="post-metadata">

**Author:** ![vic.yang](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/vic.yang/32/24656_2.png) [@vic.yang](https://devforum.zoom.us/u/vic.yang)\
**Post date:** [June 4, 2024, 2:47am UTC](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058/25 "2024-06-04T02:47:10Z")

</div>

Hey @hazzal

I am glad to hear that you have identified the cause of the issue. If you have any other questions, feel free to contact me anytime.

Thanks  
Vic

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/zoomdeveloper/original/3X/6/1/614bdd549b610bbaa46ff934617683a02bdaa03c.png) [@system](https://devforum.zoom.us/u/system)\
**Post date:** [July 4, 2024, 12:47pm UTC](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058/26 "2024-07-04T12:47:30Z")

</div>

Closed on July 4, 2024, 12:47pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

[Previous page](https://devforum.zoom.us/t/issue-creating-session-immediately-after-leaving-another-session/110058.md?page=1)
