# Socket from Unity SDK does not work properly

**URL:** <https://forum.heroiclabs.com/t/socket-from-unity-sdk-does-not-work-properly/2372>\
**Category:** Client Libraries\
**Created:** [February 17, 2022, 12:19pm UTC](https://forum.heroiclabs.com/t/socket-from-unity-sdk-does-not-work-properly/2372 "2022-02-17T12:19:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![igorshostak](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@igorshostak](https://forum.heroiclabs.com/u/igorshostak)\
**Post date:** [February 17, 2022, 12:19pm UTC](https://forum.heroiclabs.com/t/socket-from-unity-sdk-does-not-work-properly/2372/1 "2022-02-17T12:19:11Z")

</div>

Hey, Nakama specialists! I have just started using Nakama (3.3.0) in one of the mobile projects and stumbled across a problem with a socket connection. Everything seems to be working fine on a target device, but when it comes to testing in the editor, things start to get strange.

I am using this code to keep track of the connected state of the socket:

```auto
var uri = new Uri("http://example.com:7350");

var client = new Client(uri.Scheme, uri.Host, uri.Port, "defaultkey");

var session = await client.AuthenticateDeviceAsync(SystemInfo.deviceUniqueIdentifier);

var socket = Socket.From(client, new WebSocketAdapter(3, 2));

socket.Connected += () =>
{
	UnityEngine.Debug.LogError("Connected");
};

socket.Closed += () =>
{
	UnityEngine.Debug.LogError("Closed");
};

await socket.ConnectAsync(session);

```

Then while connected I just unplug my laptop from the internet, count around 3 seconds and wait for the socket to disconnect. The problem is that nothing happens for around 15-20 seconds and only after that the message “Closed” appears. Have you got any idea of why that could happen?

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom/32/676_2.png) [@tom](https://forum.heroiclabs.com/u/tom)\
**Post date:** [February 17, 2022, 2:35pm UTC](https://forum.heroiclabs.com/t/socket-from-unity-sdk-does-not-work-properly/2372/2 "2022-02-17T14:35:59Z")

</div>

Hi @igorshostak welcome to the forum.

Please can you let me know which version of Unity you’re using, as well as which version of the Nakama Server you’re using too?

---

<div class="post-metadata">

**Author:** ![igorshostak](https://avatars.discourse-cdn.com/v4/letter/i/bcef8e/32.png) [@igorshostak](https://forum.heroiclabs.com/u/igorshostak)\
**Post date:** [February 17, 2022, 2:54pm UTC](https://forum.heroiclabs.com/t/socket-from-unity-sdk-does-not-work-properly/2372/3 "2022-02-17T14:54:16Z")

</div>

Yeah, sure! I am developing stuff in `Unity 2021.2.8f1` When it comes to the server, I am running `nakama 3.10.0+ac9a4ccd`

My best guess was that something does not “click” in the WebSocketAdapter. I have found out that the websocket itself gets closed with CloseSent when PingPongManager sets it after the connection is lost. But for some reason this state does not get propagated to the Closed event of WebSocketAdapter.

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom/32/676_2.png) [@tom](https://forum.heroiclabs.com/u/tom)\
**Post date:** [February 18, 2022, 9:18am UTC](https://forum.heroiclabs.com/t/socket-from-unity-sdk-does-not-work-properly/2372/4 "2022-02-18T09:18:14Z")

</div>

Hi @igorshostak,

We’ve done some testing both with the latest `nakama-unity` package (`v3.3.0`) and the previous version (`v3.2.0`) and we’re seeing the same behaviour as you described in both. So this appears to be a pre-existing bug and not something we’ve introduced with the latest patch. Please could you raise the issue against the [`nakama-dotnet` repository](https://github.com/heroiclabs/nakama-dotnet/issues) (which is the base for the `nakama-unity` package and handles the Socket connections).

When raising the issue, if you could include the information you have gathered about where you believe the event is failing to be propagated as well as any specific reproduction steps that would be very much appreciated. We can then look into fixing this in a future release.
