Runtime Context #

All registered functions across all runtimes receive a context as the first argument. This contains fields which depend on when and how the code is executed. You can extract information about the request or the user making it from the context:

Server
1
local user_id = context.user_id
Server
1
2
3
4
userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
  // User ID not found in the context.
}
Server
1
let userId = ctx.userId;

The Go runtime context is a standard context.Context type and its fields can be accessed as shown above.

In JavaScript, context is a plain object with properties.

If you are writing your runtime code in Lua, the context will be a table from which you can access the fields directly.

Go Context KeyGo TypeLua Context KeyJavaScript Context PropertyPurpose
RUNTIME_CTX_ENVmap[string]stringenvenvA table of key/value pairs which are defined in the YAML configuration of the server. This is useful to store API keys and other secrets which may be different between servers run in production and in development.
RUNTIME_CTX_MODEstringexecution_modeexecutionModeThe mode associated with the execution context. It’s one of these values: “run_once”, “rpc”, “before”, “after”, “match”, “matchmaker”, “leaderboard_reset”, “tournament_reset”, “tournament_end”.
RUNTIME_CTX_VERSIONstringversionversionThe Nakama server version.
RUNTIME_CTX_QUERY_PARAMSmap[string]stringquery_paramsqueryParamsQuery params that was passed through from HTTP request.
RUNTIME_CTX_SESSION.IDstringsession_idsessionIdThe user session associated with the execution context.
RUNTIME_CTX_USER_IDstringuser_iduserIdThe user ID associated with the execution context.
RUNTIME_CTX_USERNAMEstringusernameusernameThe username associated with the execution context.
RUNTIME_CTX_USER_SESSION_EXPint64user_session_expuserSessionExpThe user session expiry in seconds associated with the execution context.
RUNTIME_CTX_CLIENT_IPstringclient_ipclientIpThe IP address of the client making the request.
RUNTIME_CTX_CLIENT_PORTstringclient_portclientPortThe port number of the client making the request.
RUNTIME_CTX_MATCH_IDstringmatch_idmatchIdThe match ID that is currently being executed. Only applicable to server authoritative multiplayer.
RUNTIME_CTX_MATCH_NODEstringmatch_nodematchNodeThe node ID that the match is being executed on. Only applicable to server authoritative multiplayer.
RUNTIME_CTX_MATCH_LABELstringmatch_labelmatchLabelLabels associated with the match. Only applicable to server authoritative multiplayer.
RUNTIME_CTX_MATCH_TICK_RATEintmatch_tick_ratematchTickRateTick rate defined for this match. Only applicable to server authoritative multiplayer.

Go context #

The runtime context is distinct from the Go context. It is important that a Context type be included in all server requests to avoid a potential overloading of the server with dead requests (i.e. requests from users who have since disconnected).

Inclusion of the Context allows for the context cancellation - when a user’s HTTP connection to the server is closed - to be propagated across the entire chain of requests and avoid the processing and/or buildup of such dead requests.

Related Pages