Get server time from clients

Hi

Is there a way to get a time stamp from the server on the client side (javascript)?
I could only find Lua and Go examples in the docs which I guess should be run on the server side.

If you need to get the server time from the client you should register an RPC hook with the appropriate Lua or Go code to capture server time and return it in the RPC response to the client.

Call that function in your client (in your case use client.rpcGet() in the JS client) and extract the time from the response.

An example of what the code could look like in Lua that would return the server time (UNIX time) as a response and register it with the server runtime:

local nk = require("nakama")

local function getservertime(context, payload)
    local resp = {
        current_time = os.time()
    }
    return nk.json_encode(resp)
end
nk.register_rpc("GetServerTime", getservertime)

Thanks for the replies :+1:

1 Like