Runtime module functions not getting called before/after match presence events

Hello!

For the past week, I’ve been diving in the docs and also the Nakama’s main source code trying to find a solution for a problem but haven’t been successful yet. So I thought I’d ask it here too.

Basically I’m trying to write a manager that tunes the game logic and players roles based on presence events. So if, for example, a critical role leaves the game, that manager can know what to do to help the game flow not freeze. It must be able to function properly even when clients get disconnected unexpectedly.

The main problem is that the functions registered to be called before and after “MatchLeave” in lua runtime, aren’t getting called.

The functions are quite simple and at this point, are just for testing:

local function match_leave_before(context, payload)
    nk.logger_info("[match leave before]")
    return payload
end

local function match_leave_after(context, payload)
    nk.logger_info("[match leave after]")
    return payload
end

nk.register_rt_before(match_leave_before, "MatchLeave")
nk.register_rt_after(match_leave_after, "MatchLeave")

They get registered properly:

{"level":"info","ts":"2022-03-09T10:06:43.214Z","caller":"server/runtime.go:654","msg":"Registered Lua runtime Before function invocation","id":"*rtapi.envelope_matchleave"}
{"level":"info","ts":"2022-03-09T10:06:43.214Z","caller":"server/runtime.go:668","msg":"Registered Lua runtime After function invocation","id":"*rtapi.envelope_matchleave"}

I’m testing this with some Unity clients. Tried a few times, the registered runtime functions don’t get called when I suddenly close the Unity build.
The Unity code doesn’t do anything specific in this scenario. The clients just send matchmaking requests and join the match when a match is found. The matchmaking request has no query or numeric or string properties, only a min and max room size variables.

Then I close the build to suddenly make them disconnected.

When a client suddenly gets disconnected, Nakama server realizes this and logs a presence event properly:

{"level":"debug","ts":"2022-03-09T08:51:24.712Z","caller":"server/tracker.go:907","msg":"Processing presence event","joins":0,"leaves":1}

VERSIONS:

  • Nakama 3.10.0 on Docker
  • Unity client version 2.5.1 (has been in production code base for quite some time)
  • Lua Runtime
1 Like

I think fundementally the issue is that you are not using the correct Nakama feature;

  • Before/After hooks are only invoked when a message has arrived from the client. I.e. The client calls MatchLeave client API call not when the client is disconnected, or an internal Nakama event (presence leave) is invoked.

  • What you actually need is to use Nakama’s authoritative multiplayer match handler and run your match logic authoritatively in the server. This allows you to have custom logic as part of match lifecycle hooks like match_leave callback - where the server invokes this function when it sees the client disconnect (whether they’ve called MatchLeave or they’ve disconnected abruptly).

Once you use Authoritative MP then you can achieve what you are describing in your post.

Yes! That’s what I noticed too.
I think what you said will solve the problem perefctly. I’ll look into it.

Thank you so much! :pray:t4: