Server heartbeat / loop function

Hey folks, I’m implementing a heartbeat system for my unreal servers. Bascially they call an RPC to send their status to Nakama.

I want to kick off a runtime function at the bottom of my lua file to monitor when the servers checked in but I get an ‘non-function object’ error

basically I want to call check_servers() at the end of my lua, it does the checks, then sets up a timer to repeat the process. I would prefer to use something like nk.set_timer since these are persistent servers and not matches.

local function check_servers()
nk.logger_info(“check_servers”)
update_server_status()
nk.set_timer(60 * 1000, check_servers())
end

–register rpcs

check_servers() – wouild like to start the process at the end of the file

Hello @Tommie,

Nakama’s Lua runtime doesn’t support coroutines or timers, but there’s another way you can achieve this.

You can create a custom RPC that your Unreal servers can invoke via Server-to-Server calls. In this RPC, you’d receive your heartbeats alongside some instance identifier. For each of these instances, create a Storage Object with a timestamp e.g.: value = {"last_seen": 1727365904}, collection = heartbeats, key = <instance_id>` (should be unique).

You should also setup a Storage Index for these objects. You can then use the index listing API to only return instances that have received a heartbeat in, say, the last 60 seconds (see our Query Syntax docs on how to do range queries over the timestamps).

Ideally your Unreal instances invoke some other custom RPC to delete their instance Storage Object as part of their termination lifecycle (assuming these are ephemeral instances), as the objects may pile up over time - they won’t be listed if your listing query is correct so not really a concern but still something to keep in mind for the long-run.

For “orphaned” objects (let’s say the Unreal instance failed to communicate that it was shutting down, or the message was lost) you should maybe clear up all the objects when the server restarts as part of your InitModule.

If you want samples for the custom RPCs you can have a look at our project template.

Hope this helps.