# How to implement async event tracking in JS runtime without blocking RPC response?

**URL:** https://forum.heroiclabs.com/t/how-to-implement-async-event-tracking-in-js-runtime-without-blocking-rpc-response/6264
**Category:** Runtime Framework
**Created:** [June 13, 2025, 11:19am UTC](https://forum.heroiclabs.com/t/how-to-implement-async-event-tracking-in-js-runtime-without-blocking-rpc-response/6264 "2025-06-13T11:19:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mengxin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mengxin/32/1879_2.png) [@mengxin](https://forum.heroiclabs.com/u/mengxin)
#### Post date: [June 13, 2025, 11:19am UTC](https://forum.heroiclabs.com/t/how-to-implement-async-event-tracking-in-js-runtime-without-blocking-rpc-response/6264/1 "2025-06-13T11:19:22Z")

</div>

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:**

1. In the JS custom RPC, I call:

```auto
nk.event({
  name: "analytic_event",
  properties: { action: "purchase", userId: ctx.userId },
  userId: ctx.userId,
  timestamp: Date.now(),
});

```

1. This RPC returns immediately to the client.
2. 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

---

<div class="post-metadata">

### Author: ![mengxin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mengxin/32/1879_2.png) [@mengxin](https://forum.heroiclabs.com/u/mengxin)
#### Post date: [June 13, 2025, 11:20am UTC](https://forum.heroiclabs.com/t/how-to-implement-async-event-tracking-in-js-runtime-without-blocking-rpc-response/6264/2 "2025-06-13T11:20:15Z")

</div>

base on this discussion:

> [@registerBeforeEvent and registerAfterEvent](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/9):
>
> I think there’s some confusion. Emitting new events, can be done via the client API or the provided runtime functions (in any runtime). If you register before/after hooks, they will only fire if the REST API /v2/event path receives a new event to emit, they will not if you use the runtime function inside a custom RPC. To process events, you need to register a function that will fire asynchronously on emitted events. This function can only be registered in the Go runtime.

---

<div class="post-metadata">

### Author: ![mengxin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mengxin/32/1879_2.png) [@mengxin](https://forum.heroiclabs.com/u/mengxin)
#### Post date: [June 13, 2025, 11:22am UTC](https://forum.heroiclabs.com/t/how-to-implement-async-event-tracking-in-js-runtime-without-blocking-rpc-response/6264/3 "2025-06-13T11:22:14Z")

</div>

ChatGPT give some suggestions something like

```auto
setTimeout(() => {
  nk.rpc("track_analytic_event", JSON.stringify({
    action: "purchase",
    userId: ctx.userId
  }));
}, 0);

```

Does this code work?

---

<div class="post-metadata">

### Author: ![chris1](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@chris1](https://forum.heroiclabs.com/u/chris1)
#### Post date: [June 19, 2025, 4:35pm UTC](https://forum.heroiclabs.com/t/how-to-implement-async-event-tracking-in-js-runtime-without-blocking-rpc-response/6264/4 "2025-06-19T16:35:23Z")

</div>

Yes, this pattern is valid.

The Go runtime can subscribe to events created by the JS runtime using nk.event. That’s currently the only way to do it as far as I know. We do exactly this for sending events to Mixpanel asynchronously.
