Skip to content

t1k:nakama:rpc

FieldValue
Modulebase
Version1.6.2
Effortlow
Tools

Keywords: gRPC, JSON, nakama, proxy, remote procedure call, rpc, server

/t1k:nakama:rpc

RPCs are the primary client-server communication channel. Each RPC receives a JSON string payload from the client and returns a JSON string response. In the proxy architecture, RPCs convert JSON to gRPC requests, call the Gameplay service, and convert responses back.

initializer.RegisterRpc("rpc_name", handlerFunc)
func handler(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
// 1. Check gRPC connection
// 2. Parse JSON payload
// 3. Extract user ID from ctx
// 4. Convert to gRPC request
// 5. Call Gameplay service
// 6. Convert response to JSON
// 7. Return JSON string
}
if !client.IsConnected() {
return "", runtime.NewError("gameplay service unavailable", 14) // UNAVAILABLE
}
userID, _ := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
// ... build gRPC request with userID
resp, err := client.SomeMethod(ctx, req)
if err != nil {
return "", mapGRPCError(err)
}
jsonBytes, _ := json.Marshal(convertResponse(resp))
return string(jsonBytes), nil

gRPC status codes map to Nakama runtime error codes:

  • codes.NotFoundruntime.NewError(msg, 5) (NOT_FOUND)
  • codes.InvalidArgumentruntime.NewError(msg, 3) (INVALID_ARGUMENT)
  • codes.Unavailableruntime.NewError(msg, 14) (UNAVAILABLE)
  • codes.Internalruntime.NewError(msg, 13) (INTERNAL)
  • RPC payload is a raw JSON string — always validate/unmarshal before use
  • Return empty string "" with error for failures, not ("{}", error)
  • RPC names must be globally unique across all registered RPCs
  • Context keys like RUNTIME_CTX_USER_ID may be empty for unauthenticated RPCs
  • Always check client.IsConnected() before gRPC calls
  • Unexported fields: Go’s JSON marshaling silently skips unexported struct fields. Always use uppercase field names or json:"tag".
  • gRPC deadline propagation: Always propagate context deadlines from Nakama to gRPC calls. Missing deadlines cause requests to hang forever.
  • Context cancellation: Check ctx.Err() before expensive operations. Client disconnects cancel the context.
  • RPC registered in InitModule via initializer.RegisterRpc
  • Handler validates payload before processing
  • User ID extracted from context where needed
  • gRPC errors mapped to appropriate Nakama error codes
  • Response serialized as compact JSON string
  • Mock endpoint added in gameplay_mock.go if applicable