Server function on socket disconnect

Hello,

Is there anyway for the server to run a function (either Lua or Go) on client socket disconnect? I’d like to do some cleanup since players will be interacting with a third party service indirectly via RPC.

It’s not documented yet but you can register an event handler in the Go runtime that triggers when a session ends for any reason: connection lost, user disconnected and so on.

2 Likes

@zyro, just wondering, Is there any equivalent for this in Lua runtime?

No, there’s currently no Lua equivalent for the event functions. You can mix and match the runtimes, so use these Go-based functions alongside your existing Lua modules.

Hi any specific reason that the nk runtime.NakamaModule, db *sql.DB are not made available in this function?

Yes - we specifically want to prohibit developers interacting with the database or Nakama subsystems in these event hooks. This encourages bad design and could actually end up being a DDoS vector when many users connect/disconnect from the system (say in a bad networking scenario - stampeding herd problem: https://en.wikipedia.org/wiki/Thundering_herd_problem)

Makes sense. Thanks!