How to get specific user id in a match?

How do we get a specific user id in a match in order to send specific messages to that specific player ?

I tried:

presences[1].userId //Stops the match by throwing an error

s.presences[1].userId //Same

Object.keys(s.presences) // I can get the length with Object.keys(s.presences).length so I thought by asking for an index it would work but no it would not let me compile this :

@atv You don’t specify a user ID you specify a presence (or list of presences) or no presences (to send to all). The reason it’s a presence is because a presence consists of this internal structure {node_id, user_id, session_id} .

It allows the game server to disambiguate between two different socket connections opened by the same user (one of which might be used for the multiplayer and the other for chat messages - as an example).

You can see an example of this usage:

nakama-project-template/match_handler.ts at master · heroiclabs/nakama-project-template · GitHub

Hope that helps.

Thank you. I’ve managed to hack around what I wanted to do with the message.sender

@atv I’m glad we could help. What sort of hack did you need to use to achieve what you want? The API already lets you specify the exact presences you want to broadcast a message at. I’m curious what code you used and if we can make it clearer.

Well actually, my solution only works halfway,

If a client sends a message and I need to send to that exact client a specific message right afterwards, I can use the message.sender ;

But if I need to dispatch a message after some other stuff, I don’t know how to get the presence of the specific user I’m looking for.

I understand this and it works :

dispatcher.broadcastMessage(opCode, JSON.stringify(response),[message.sender]);

The presence is the message.sender, the client sends something, I can immediatly answer to him specifically

But if I need to send him a message for other reasons when the client has not send something before, I don’t understand what to use instead of message.sender.

I tried this :


But it stops the match with the error error":"TypeError: expects a valid set of presences

Long story short, when the two players are joining a match, the first thing they do is to ask the server to receive a playerID, be 1 or 2. On top of that, I can access their userID, username, sessionID and node through the message.sender that I use to set equivalent variables like player01UserIDString, player01Username etc…

I did this in order to try to do the following to send messages :

 dispatcher.broadcastMessage(opCodePlayerToStart, JSON.stringify(''),[s.presences{userId:s.player01IDString,sessionId:s.player01SessionID,username:s.player01Username,node:s.player01Node}]);

But it won’t let me compile, saying that Type ‘{ [userId: string]: Presence; }’ is missing the following properties from type ‘Presence’: userId, sessionId, username, node

Tried also to create a player01Presence:nkruntime.Presence variable type in the state so that each client would set it at a specific moment different from match init, but at the match init when state is initialized, I tried to make player01Presence:null and it would not let me saying it can’t be null.

So anyway, I still don’t exactly understand how to send a message to a specific player that is not a previous message.sender and I did not find that specific thing in the github example, all messages send to clients are through message.sender but I need to send messages to specific client that have not started the conversation in the first place.

I seems to have solved it by creating two variables outside of state :

let player01Presence:nkruntime.Presence;
let player02Presence:nkruntime.Presence;

When my players asks for their ID I use the message.sender to set their respective presence and then I can use these to send them messages

dispatcher.broadcastMessage(opCodePlayerToStart, JSON.stringify(''),[player01Presence]);

It works now but don’t hesitate if there’s a more elegant way to do it, thanks.