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.

1 Like

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)
1 Like

Thanks for the replies :+1:

1 Like
os.time()

will give time in seconds. Is there a possible way to get it in milliseconds?

@Mohammed We have a helper function on the Nakama module in the Lua runtime.

local nk = require("nakama")
print(nk.time()) -- prints time in milliseconds since UNIX epoch

You can find it in the function reference:

https://heroiclabs.com/docs/runtime-code-function-reference/#time

you 're a lifesaver man, I owe you big time

1 Like