Typescript Nakama runtime does not support async functions for custom rpcs

I’ve been trying to understand how the typescript nakama runtime handles concurrency. From the docs it says:

Single threaded

The use of multi-threaded processing is not supported in the JavaScript runtime.

When I try to add a custom rpc that is async it does not allow me to.


let asyncRpcFunction: nkruntime.RpcFunction = async (context: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string): Promise<string> => {
    // Asynchronous logic here
    return "Async Response";  // This should be inside a Promise.resolve if not using async/await syntax directly.

let initModule: nkruntime.InitModule = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
    initializer.registerRpc("my_async_rpc", asyncRpcFunction);

Error: Cannot register a function that returns a Promise from registerRpc.

Does nakama typescript runtime handle execution asynchronously by default or is it something I have to manage in my custom rpc function definitions with async/await?

The docs/examples only include a synchronously defined custom rpc function.

Hello @lukep,

Nakama JS is ES5 only which does not have support for async/await.

You do not need to worry about async execution, concurrency is handled by Nakama using a pool of VMs, see the appropriate configurations here.

Hope this clarifies.

Hey @sesposito - Thank you for the clarification and the quick response. That’s really helpful info. I would encourage you guys to call that out more somewhere in the docs (maybe in the code example) just to alleviate concerns. I figured there had to be a thread pool.

Separately - I’ve been running into this issue where I can’t use an npm package to verify a JWT because the typescript runtime is using goja vm under the hood and most npm packages rely on Node js runtime (crypto package specifically).

Have you run into that issue before and do you have any recommended packages to use with the typescript nakama runtime to accomplish jwt verification? I’d really like to avoid making my own custom jwt verification logic if possible since that would be painful.

As you correctly point out, Nakama is not a Node.js environment so packages that rely on it won’t work.

We don’t have recommendations for a JS JWT validation package or others, what you could do is use the Go runtime to expose an RPC to validate the tokens (should be fairly easy) and invoke that RPC in your JS custom code via an HTTP call to localhost.

Have a look at our project-template for an example project in TS and Go.

Hope this helps.