# registerBeforeEvent and registerAfterEvent

**URL:** <https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943>\
**Category:** Runtime Framework\
**Tags:** server-framework\
**Created:** [January 17, 2025, 3:12pm UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943 "2025-01-17T15:12:47Z")\
**Posts on this page:** 8\
**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:** [January 17, 2025, 3:12pm UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/1 "2025-01-17T15:12:47Z")

</div>

May I ask about the purpose of these two registrations in the JS runtime (`registerBeforeEvent`and `registerAfterEvent`)? There doesn’t seem to be any documentation about them. The comment mentions “Register before Hook for RPC Event function,” but based on testing, the logic doesn’t seem to execute after the RPC call.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [January 21, 2025, 3:44pm UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/2 "2025-01-21T15:44:54Z")

</div>

Hello @mengxin,

I managed to successfully register and execute both hooks after invoking the [Event API](https://heroiclabs.com/docs/nakama/api/client/index.html#/Nakama/Nakama_Event).

---

<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:** [February 24, 2025, 10:59am UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/4 "2025-02-24T10:59:02Z")

</div>

ah so the event in these 2 runtime API is for client event right?

1. the sever register event hook: before and after
2. the client sends an event
3. the hooks run

for step2 , is it possible for **the server sends events**?

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [February 24, 2025, 11:03am UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/5 "2025-02-24T11:03:46Z")

</div>

The hooks only run for the client-facing standard APIs, not for custom RPCs.

There’s a runtime function to emit events, so you can wrap custom logic in an RPC you define.

I hope this clarifies.

---

<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:** [February 24, 2025, 3:34pm UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/6 "2025-02-24T15:34:40Z")

</div>

sorry, could you tell what the runtime function is? and i am not sure understand this part:

> so you can wrap custom logic in an RPC you define.

what i want to achieve is

1. register hook E1
2. some RPC send and process, in the logic, emmit an event E1 and RPC finish
3. the E1 event hook is triggered async and runs some logic, such as unlocking some function

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [February 24, 2025, 3:47pm UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/7 "2025-02-24T15:47:29Z")

</div>

> **[Function Reference](https://heroiclabs.com/docs/nakama/server-framework/typescript-runtime/function-reference/#event)**
>
> This page lists all functions available within Nakama's JavaScript runtime, with their respective parameters and corresponding code samples.

---

<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:** [February 24, 2025, 3:52pm UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/8 "2025-02-24T15:52:35Z")

</div>

I tested this api

1. i add this logic in one RPC

```auto
  const properties = {
    my_key: 'my_value',
  }
nk.event('account_updated', properties)
nk.event('other_event', properties)

```

1. register hook

```auto
 initializer.registerBeforeEvent(beforeHookFunction)
initializer.registerAfterEvent(afterHookFunction)

```

```auto
const beforeHookFunction: BeforeHookFunction<nkruntime.Event> = function (
  ctx: Context,
  logger: Logger,
  nk: Nakama,
  data: nkruntime.Event,
) {
  logger.debug(`beforeHookFunction ${JSON.stringify(data)}`)
}

const afterHookFunction: AfterHookFunction<void, nkruntime.Event> = function (
  ctx: Context,
  logger: Logger,
  nk: Nakama,
  data: void,
  request: nkruntime.Event,
) {
  logger.debug(`afterHookFunction ${JSON.stringify(request)}`)
}

```

After i invoke the RPC, i suppose to see the log: beforeHookFunction and afterHookFunction, but it seems doesnt work.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [February 24, 2025, 4:28pm UTC](https://forum.heroiclabs.com/t/registerbeforeevent-and-registerafterevent/5943/9 "2025-02-24T16:28:00Z")

</div>

I think there’s some confusion.

> **[Events](https://heroiclabs.com/docs/nakama/concepts/events/#process-events)**
>
> Events are a powerful way to send data to the game server and create data to be processed in the background on the server. You can create and receive data which can be forwarded to 3rd-party services like Analytics, Ads, In-app Purchases, and more.

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