# Issue of unavailable thread context for main-thread-only unity functionalities when called from Namama Socket.ReceiveLoop() async events

**URL:** <https://forum.heroiclabs.com/t/issue-of-unavailable-thread-context-for-main-thread-only-unity-functionalities-when-called-from-namama-socket-receiveloop-async-events/578>\
**Category:** Local Setup\
**Created:** [March 22, 2020, 12:49pm UTC](https://forum.heroiclabs.com/t/issue-of-unavailable-thread-context-for-main-thread-only-unity-functionalities-when-called-from-namama-socket-receiveloop-async-events/578 "2020-03-22T12:49:18Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Mahdad-Baghani](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mahdad-baghani/32/465_2.png) [@Mahdad-Baghani](https://forum.heroiclabs.com/u/Mahdad-Baghani)\
**Post date:** [March 22, 2020, 12:58pm UTC](https://forum.heroiclabs.com/t/issue-of-unavailable-thread-context-for-main-thread-only-unity-functionalities-when-called-from-namama-socket-receiveloop-async-events/578/2 "2020-03-22T12:58:52Z")

</div>

The current answer by @novabyte:

You can use the synchronization context from the Unity main thread when you need to do it:

[https://github.com/heroiclabs/nakama-unity/blob/master/Assets/Nakama/Snippets/NakamaManager.cs#L108](https://github.com/heroiclabs/nakama-unity/blob/master/Assets/Nakama/Snippets/NakamaManager.cs#L108)

Alternatively you can use a dispatch queue to move messages from the socket thread onto the Unity main thread. Either pattern works.

**update**

I simply ended up using the second approach. I found a great repository on GitHub named [UnityMainThreadDispatcher](https://github.com/PimDeWitte/UnityMainThreadDispatcher) and all I have to do now inside my handler subscribed to the async event of `Socket`:

```auto
UnityMainThreadDispatcher.Instance().Enqueue(() => 
{
   //some code that needs to be run on unity's main thread 
});

```

---

_[View the full topic](https://forum.heroiclabs.com/t/issue-of-unavailable-thread-context-for-main-thread-only-unity-functionalities-when-called-from-namama-socket-receiveloop-async-events/578)._
