How to send Direct Message?


I am trying to integrate Nakama into my game. I ran into a little trouble on sending direct message. I use C++ and used the example code from the documentation shown below. After the JoinChat request was sent, a notification of DM invitation was sent to the receiver. The receiver did receive the notification, but I do not know how to reply that notification to accept the invitation, I do not find any documentation on this detail. I tried to confirm by replying with the same Joinchat request to the sender. But it seems not working as I tried to send messages afterwards by using writeChatMessage API to the granted private channel, the receiver can not receive the those direct messages. Anyone can help or an example code for this? Thanks!

auto successCallback = [](NChannelPtr channel)
    std::cout << "You can now send messages to channel id: " << channel->id << std::endl;

string userId = "<user id>";
rtClient->joinChat(userId, NChannelType::DIRECT_MESSAGE, true, false, successCallback);

Just for anyone who had similar questions, I did found the solution by myself. It seems to me that the receiver’s responding with the same JoinChat request is the right way to accept the DM invitation, but I do not find this in any official documentation. Failing to send private messages was due to something else actually which is how I got confused. Maybe it is nice to make invitation responding mechanism clear in the documentation as in general the documentation is quite good and in detail.

Yep, something like that


I have one more question - How to receive direct messages after reconnect.
For example: Alice and Bob connected to direct channel and send each other messages - everything work as expected.
Bob disconnected from server and connected again. Alice still connected to channel and send messages to Bob. But Bob received nothing (No channel messages, no events). How to receive messages on Bob side? We should save all direct chat channels IDs and connect manually?

Correct, to receive messages while disconnected (offline) they should be persisted, and when the disconnected user comes back it should use Nakama: Real-time Chat | Heroic Labs Documentation to list the “missed” messages and re-join the channel to receive any messages sent thereafter.

1 Like

Now I see the documentation is updated and it is very clear now :+1:

1 Like