I’m currently using the JavaScript runtime in Nakama , and I want to implement event tracking (e.g., sending to Mixpanel) when a custom RPC is called. However, I want to return the RPC response immediately to the client and send the tracking event asynchronously in the background , without blocking the response.
I understand that the JS runtime does not allow us to register custom event handlers for nk.event(…). So I was thinking of the following workaround — could you please confirm if this approach is correct or suggest a better one?
Proposed Flow:
- In the JS custom RPC, I call:
nk.event({
name: "analytic_event",
properties: { action: "purchase", userId: ctx.userId },
userId: ctx.userId,
timestamp: Date.now(),
});
- This RPC returns immediately to the client.
- On the Go runtime side, I register a handler for this custom event (analytic_event), and inside it, I call external services like Mixpanel to track the event.
Questions:
- Is this pattern valid?
- Can Go runtime subscribe to events created by JS runtime using nk.event()?
- Is there a more elegant way to achieve async side effects after an RPC call in JS? (return rpc response to client fast and do some action after return to client)
Thanks