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.
Hello @mengxin,
I managed to successfully register and execute both hooks after invoking the Event API.
ah so the event in these 2 runtime API is for client event right?
- the sever register event hook: before and after
- the client sends an event
- the hooks run
for step2 , is it possible for the server sends events?
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.
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
- register hook E1
- some RPC send and process, in the logic, emmit an event E1 and RPC finish
- the E1 event hook is triggered async and runs some logic, such as unlocking some function
I tested this api
- i add this logic in one RPC
const properties = {
my_key: 'my_value',
}
nk.event('account_updated', properties)
nk.event('other_event', properties)
- register hook
initializer.registerBeforeEvent(beforeHookFunction)
initializer.registerAfterEvent(afterHookFunction)
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.
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.