Can I use runtime.RUNTIME_CTX_VARS to pass value to server?

I want to pass an app version to nakama server in every connection.
I tried to store it in ISession.

session.Vars.Add("AppVersion", "this is test value");

and call rpc like this.

var apiRpc = await client.RpcAsync(session, "GetPlayerInfoRPC", request);

in gomodules.
I tried to get it via context.

vars, ok := ctx.Value(runtime.RUNTIME_CTX_VARS).(map[string]string)

But ok is always false.

What is the Vars for?
And What is the best way to pass custom values in all connections?

Hey @opus_kojiro. We’ve not caught up with documentation yet but a quick explanation on what session variables are for will help you with your Nakama development.

Session variables allow server side code to embed additional key/value pairs into the session token generated by the game server. This enables the session token to act like an edge cache for information that won’t change until the next authentication (or re-authentication) happens.

There’s two ways to set session variables:

  1. You can send them from the game client when the authenticate request is made to the server. For example with the Unity client sdk:

    var id = "some-unique-identifier";
    string username = null;
    var create = true;
    var vars = new Dictionary<string, string> {{"key", "value"}};
    var session = await _client.AuthenticateCustomAsync(id, username, create, vars);
  2. Set the session variables on the server-side with a before hook on the authenticate feature you use:

    func beforeAuthenticateCustom(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateCustomRequest) (*api.AuthenticateCustomRequest, error) {
        in.GetAccount().Vars = map[string]string{"key": "value"}
        return in, nil

When you’ve set some session variables you’ll be able to access them from the session object as you’ve found in the Unity client sdk. You can also obtain them server side as you’ve shown via the context object with either Go or Lua scripts.

The use case you have which is to check whether the game client version is within some range is an example of a good reason we introduced the concept of session variables. Hope this helps. :slight_smile:

1 Like

Worked fine!

1 Like