WriteChatMessageAsync stalls forever

Hey,

I’m new to Nakama, using Unity and Nakama in combination.

I got everything working well, except when I try to send a chat message. The socket is connected, the chat is joined.

Here’s some extremely ugly test code:

        await authTask;

    var list = _client.ListGroupsAsync(_session, "Test Clan");
    await list;
    var r = list.Result;
    foreach (var g in r.Groups)
    {
        groupID = g.Id;
        break;
    }

    await _client.JoinGroupAsync(_session, groupID);

    Socket = _client.NewSocket();

    Socket.Connected += () =>
    {
        Debug.Log("Socket connected.");
    };
    Socket.Closed += () =>
    {
        Debug.Log("Socket closed.");
    };
    
    await Socket.ConnectAsync(_session, true);

    var b =await Socket.JoinChatAsync(groupID, ChannelType.Group, true);
    Debug.Log(b.ToString());
    Debug.Log("Chat joined!");
    
    var content = new Dictionary<string, string> {{"Message", "Hello!"}}.ToJson();
    await Socket.WriteChatMessageAsync(groupID, content);

    Debug.Log("Please get to his point!");

So it never gets to the last debug statement. WriteChatMessageAsync stalls forever, no error is showing on the server log. And when I join the group, it works fine and it shows everyone online correctly.

Any clues what’s up?

Thanks!

@nixarn That’s strange :thinking: what version of the Unity client do you use? Also let me know what version of the game server you’ve set up.

Hey thanks for getting back to me!

The Unity client is 2.7.0 and Nakama is 2.12.0+650b506a. (copied from the log).

I started playing around with it yesterday, so maybe I’m missing something important? The authentication works, copied from your site and looking at the nakama admin it has created the users & the group correctly. It’s just the WriteChatMessageAsync that doesn’t work and stalls forever.

EDIT: I’m gonna try using the javascript client, and do the same thing and see what happens

@nixarn I’ll give your code a quick try as a test. :+1:

Sound great, and if you want I can send over everything, it’s not much more than what you got already though! Thanks! I tried a quick javascript test but maybe the javascript lib on github (not using NPM) is way out of date, it was from 2018. And I get ‘TypeError: socket.joinChat is not a function’ and looking at the source I couldn’t find a joinChat function. Could be socket.joinChat works for TypeScript? I’ve never really used TS, so a bit lost there…

I got it working!

So I used the group’s ip as the chat id. I thought that would work as the group chat, not sure where I got that from. But when I then changed the groupid to the id fetched from the join call, which is the channel id, then it started working.

Thanks for looking into it!

@nixarn I was just about to write a response to you :smile:

I used one of our test snippets of code and could not reproduce the issue but it looks like you’ve identified root cause already.

https://github.com/heroiclabs/nakama-unity/blob/master/Packages/Nakama/Editor/Snippets/RealtimeChatRoom.cs

I do think that an error should have been thrown if the chat message could not be sent to the channel because the ID was invalid. Do you see an Exception object somewhere in the Unity logs?

I didn’t get any errors showing up in the log. I’m new to async programming in C# so could be the fault is my side.

Thanks a lot f the snippet, great to learn from!