[RPC] Calling custom rpc fuction sessionId missing

Hi pro,

I am back again. Got some issues during developing a custom RPC function.

It’s about the streaming service. While joining the stream, I use ctx.sessionId to get the session id but get undefined. And add user to stream requires attaching a session id (can be sent manually during request rpc function, but why?)

This is my code for joining stream:

function playerJoinStream(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: any) {
  try {
    let streamId: nkruntime.Stream = joinStreamId;
    logger.info("*+*+*+*+*+*+*+")
    logger.info("trigger player join stream");
    logger.info("ctx json:" + JSON.stringify(ctx));
    // logger.info("ctx.sessionId:" + ctx.sessionId);
    logger.info("trigger player join stream");

    logger.info("*+*+*+*+*+*+*+")
    nk.streamUserJoin(ctx.userId, ctx.sessionId, streamId);

  } catch (error) {
    logger.info(JSON.stringify(error))
  }
}

and this is the log

nakama_1       | {"level":"info","ts":"2022-06-01T09:12:31.201Z","caller":"server/runtime_javascript_logger.go:68","msg":"*+*+*+*+*+*+*+"}
nakama_1       | {"level":"info","ts":"2022-06-01T09:12:31.202Z","caller":"server/runtime_javascript_logger.go:68","msg":"trigger player join stream"}
nakama_1       | {"level":"info","ts":"2022-06-01T09:12:31.202Z","caller":"server/runtime_javascript_logger.go:68","msg":"ctx json:{\"node\":\"nakama1\",\"env\":{},\"executionMode\":2,\"queryParams\":{},\"userSessionExp\":1654081898,\"userId\":\"188d0185-b8f1-4df6-81c8-8af1d11c6321\",\"username\":\"testtest\",\"clientIp\":\"172.19.0.1\",\"clientPort\":\"63686\"}"}

As the log shows above, ctx not contain the sessionId.
Is anything I missed out?

Add on details: client is able to join chat channel

1 Like

Hi @kennot,

Are you calling the RPC function over a socket?
You will only receive a session ID if communicating though a socket connection, as the socket contains the session ID for the socket handle managed by the game server.

1 Like

hi @ftkg,

Thanks your advice, I found out there is another example code from the documentation after client.rpc(). Which is socket.rpc(), now i can fetching sessionId without any issues.

Hi pro,
I got some additional questions regarding the join/leave stream.

There is a method to make the client leave the stream by using streamUserLeave .
How about when clients disconnect or close the game before triggering streamUserLeave?

On disconnects, the user will be automatically removed from any streams it was part of, as described in the docs: Nakama: Streams | Heroic Labs Documentation

Also please start a new topic if you have further unrelated questions.