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:
|
|
|
|
|
|
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 Key | Go Type | Lua Context Key | JavaScript Context Property | Purpose |
---|---|---|---|---|
RUNTIME_CTX_ENV | map[string]string | env | env | A 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_MODE | string | execution_mode | executionMode | The 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_NODE | string | node | node | The node ID where the current runtime context is executing. |
RUNTIME_CTX_VERSION | string | version | version | The server version. |
RUNTIME_CTX_HEADERS | map[string][]string | headers | headers | The Nakama server version. |
RUNTIME_CTX_QUERY_PARAMS | map[string][]string | query_params | queryParams | Query params that was passed through from HTTP request. |
RUNTIME_CTX_SESSION_ID | string | session_id | sessionId | The user session associated with the execution context. This value is only present for requests coming from socket connections. |
RUNTIME_CTX_USER_ID | string | user_id | userId | The user ID associated with the execution context. This value is not present in Server-To-Server calls. |
RUNTIME_CTX_VARS | map[string]string | vars | vars | Variables stored in the user’s session token. |
RUNTIME_CTX_USERNAME | string | username | username | The username associated with the execution context. This value is not present in Server-To-Server calls. |
RUNTIME_CTX_USER_SESSION_EXP | int64 | user_session_exp | userSessionExp | The user session expiry in seconds associated with the execution context. |
RUNTIME_CTX_LANG | string | lang | lang | The user session’s lang value, if one is set. |
RUNTIME_CTX_CLIENT_IP | string | client_ip | clientIp | The IP address of the client making the request. |
RUNTIME_CTX_CLIENT_PORT | string | client_port | clientPort | The port number of the client making the request. |
RUNTIME_CTX_MATCH_ID | string | match_id | matchId | The match ID that is currently being executed. Only applicable to server authoritative multiplayer. |
RUNTIME_CTX_MATCH_NODE | string | match_node | matchNode | The node ID that the match is being executed on. Only applicable to server authoritative multiplayer. |
RUNTIME_CTX_MATCH_LABEL | string | match_label | matchLabel | Labels associated with the match. Only applicable to server authoritative multiplayer. |
RUNTIME_CTX_MATCH_TICK_RATE | int | match_tick_rate | matchTickRate | Tick 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.