# Hi i need help with unity webgl socket.ConnectAsync throwing error on webgl

**URL:** <https://forum.heroiclabs.com/t/hi-i-need-help-with-unity-webgl-socket-connectasync-throwing-error-on-webgl/4302>\
**Category:** Game Design\
**Tags:** getting-started, sdk-unity\
**Created:** [September 5, 2023, 6:49pm UTC](https://forum.heroiclabs.com/t/hi-i-need-help-with-unity-webgl-socket-connectasync-throwing-error-on-webgl/4302 "2023-09-05T18:49:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![manirana94](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@manirana94](https://forum.heroiclabs.com/u/manirana94)\
**Post date:** [September 5, 2023, 6:49pm UTC](https://forum.heroiclabs.com/t/hi-i-need-help-with-unity-webgl-socket-connectasync-throwing-error-on-webgl/4302/1 "2023-09-05T18:49:50Z")

</div>

Hi i need help with unity webgl socket.ConnectAsync throwing error on WebGL

WebSocket error: undefined

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [September 5, 2023, 9:09pm UTC](https://forum.heroiclabs.com/t/hi-i-need-help-with-unity-webgl-socket-connectasync-throwing-error-on-webgl/4302/2 "2023-09-05T21:09:20Z")

</div>

@manirana94 can you share the code you use to set up your socket in Unity WebGL builds?

For example did you [follow the instructions](https://github.com/heroiclabs/nakama-unity#unity-webgl) and use code like:

```plaintext
#if UNITY_WEBGL && !UNITY_EDITOR
    ISocketAdapter adapter = new JsWebSocketAdapter();
#else
    ISocketAdapter adapter = new WebSocketAdapter();
#endif
var socket = Socket.From(client, adapter);

```

---

<div class="post-metadata">

**Author:** ![manirana94](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@manirana94](https://forum.heroiclabs.com/u/manirana94)\
**Post date:** [September 6, 2023, 7:53am UTC](https://forum.heroiclabs.com/t/hi-i-need-help-with-unity-webgl-socket-connectasync-throwing-error-on-webgl/4302/3 "2023-09-06T07:53:02Z")

</div>

@novabyte Thank you for helping here is my code for socket connection.

```auto
 _client = new Client(scheme, host, port, serverKey, UnityWebRequestAdapter.Instance);
            _socket = _client.NewSocket();

            #if UNITY_WEBGL && !UNITY_EDITOR
            ISocketAdapter adapter = new JsWebSocketAdapter();
            #else
            ISocketAdapter adapter = new WebSocketAdapter();
            #endif
            _socket = Socket.From(_client, adapter);

            _socket.Closed += () => Debug.Log("Socket closed.");
            _socket.Connected += () => Debug.Log("Socket connected.");
            _socket.ReceivedError += e => Debug.Log("Socket error: " + e.Message);
            
            var udid = Guid.NewGuid().ToString();

            var sessionToken = NetworkUtils.GetAuthToken();
            if (string.IsNullOrEmpty(sessionToken) || (_session = Session.Restore(sessionToken)).IsExpired)
            {
                _session = await _client.AuthenticateDeviceAsync(udid);
                NetworkUtils.SaveDeviceID(udid);
                NetworkUtils.SaveAuthToken(_session.AuthToken);
            }
            await _socket.ConnectAsync(_session, true);

```

I got an error on \_socket.ConnectAsync its return error WebSocket error: undefined on WebGL build
