What do I use instead of setTimeout in the JS Runtime?

Apparently, Goja doesn’t support Javascript setTimeouts (nor any NodeJS functionality) and in the Nakama Javascript ambient theres no setTimeout defined, so what can I use/do instead?

I’ve tried a couple of solutions suggested by ChatGPT:

  1. Creating a Go function to mimic setTimeout and exposing it to the JavaScript environment. (impractical)
  2. Recreating setTimeout in JavaScript using an event queue and a basic event loop.

Unfortunately, the second solution didn’t work, likely due to limitations within Goja.

I really need a function similar to setTimeout to implement a rate-limiting solution with token expiry scheduling for some Socket RPC messages in Nakama. These limitations of Goja are making me skeptical about the effectiveness of using the TypeScript runtime as recommended in the official documentation :smiling_face_with_tear:.

Given these challenges, I am beginning to think that using the Go runtime might be more effective and could potentially reduce my development time. I’ve already faced difficulties patching some npm packages to be compatible, and now the lack of support for global variables in the Typescript runtime is going to complicate the ratelimit logic a bit.

Simple tasks that should be straightforward are taking up some significant development time due to these limitations. Could anyone suggest a workaround or an alternative approach? Or do you think switching to the Go runtime would be a better option for using external dependencies? Or if the additional work to make things working in the TypeScript runtime is worth it?

Thank you in advance for your help and insights

Hello @Suero,

This entirely depends on the dependencies that you need. You can mix usage of the JS and Go runtimes, so you could implement your rate-limiting before hooks in Go instead, but despite my previous suggestion, I still think this should be done by a load balancer.

Can you elaborate on the motivation for having to implement rate-limiting at the server level?

Best.

1 Like

I want to set ratelimit in RPCs that are done with the Nakama Socket ( not normal HTTP requests afaik ) , so, I didn’t find any solution to ratelimit different websocket messages with different ratelimits at the LB level.

So, my plan now, is to create a ratelimit logic for Socket RPCs in the server code, and use the LB everywhere is possible to manage things. Mixing both JS and Golang runtimes can also be a great idea, thanks for saying.