User Nakama ID

How client should find the user id stored in database?

@SinaZK Can you add more detail to your question? Each user is assigned an ID when their account is created. What do you mean “find the user id stored”? Find based on what?

I mean every chat message has sender_id and i want to separate my messages and other group messages and i want to know my nakama user id. Another example is when i want to list my groups i need nakama user id.

We use authenticate device for auth and it just respond the token not the user id.

@SinaZK The session object in Nakama will give you back the user ID:

https://github.com/heroiclabs/nakama-dotnet/blob/master/src/Nakama/ISession.cs#L65

This is covered with examples in the Unity client sdk guide:

https://heroiclabs.com/docs/unity-client-guide/#sessions

You can also access the user ID from the context object in server-side logic. For Lua it looks like:

local nk = require("nakama")
local function custom_rpc_func(context, payload)
    nk.logger_info(("User id: %q"):format(context.user_id))
end
nk.register_rpc(custom_rpc_func, "some_id")

And in Go code:

func CustomRpcFunc(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
    userID, _ := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
    logger.Info("User id: %s", userID)
    return "", nil
}

All of this is covered in the documentation. Hope it helps.

1 Like

Tnx. Ä° implemented a rpc func and passed Id to client.

@SinaZK This is not needed. I do not recommend this approach because it complicates your game client code and is wholly unnecessary because the client sdk already has the user ID as soon as the user has authenticated to obtain a session.

Ok I’m going to use the session. Tnx for your replies.

1 Like

what if i want to send a notification from one client to other client trough server rpc? im using go and usernames , but now i dont see how to get the user id from server using username, it this possible? so i can send a notification to him thanks

@PabloMonfort. You can use one of the server framework functions to get the user ID for one or more users via their username:

https://heroiclabs.com/docs/runtime-code-function-reference/#users

In Go it’s called nk.UsersGetUsername and in Lua its nk.users_get_username. There’s a code example for usage in the documentation above.

is not this kind Twisted! ? nk.UsersGetUsername and nk.UsersGetId ? ok so i get it UsersGetUsername retrieves the ID … but why? should not be UsersGetId to retrieve the id? thanks for the answear :smiley:

NVM i think i get the concept, we get the user via Username so thats why UsersGetUsername it has sense. but its look twisted in some way if we want to get the ID using Username lol but i understand it , thanks for clarify me this

@PabloMonfort It’s not twisted. If you read the description in the function reference you’ll see that UsersGetUsername is used to obtain the user object for one or more users so it includes lots more fields than just the user ID.

In general there’s no where in the game server where you’d have the username for a user but not also have their user ID. So it’s strange that you need to do this lookup in the first place. I just assumed you’d cached the information for some reason. Why do you have a username for a player but don’t have their user ID?

coz he is sending a request to one of his friends, i dont save the other user ids on his client in case of dunno , security? i just ignore the other userids from clients but handle it via username

@PabloMonfort Ok, you stripped out the user ID even though its safe to send over the network to players and then wondered why it seems more difficult than you’d expect to use the user ID with the notifications feature. :slight_smile:

It’s up to you whether you hold onto the user ID on the device but there’s no harm in it. The API we’ve built is well designed and notifications are specifically required to be sent via custom RPC functions to encourage input validation and protect against bad users in your game. You have full control over that logic.

i just solve the problem at my end thanks