# Error during implementing before realtime hooks

**URL:** https://forum.heroiclabs.com/t/error-during-implementing-before-realtime-hooks/3225
**Category:** Runtime Framework
**Tags:** typescript, server-framework
**Created:** [November 2, 2022, 9:17am UTC](https://forum.heroiclabs.com/t/error-during-implementing-before-realtime-hooks/3225 "2022-11-02T09:17:22Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Shahariar\_Ashik](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Shahariar\_Ashik](https://forum.heroiclabs.com/u/Shahariar_Ashik)
#### Post date: [November 2, 2022, 9:17am UTC](https://forum.heroiclabs.com/t/error-during-implementing-before-realtime-hooks/3225/1 "2022-11-02T09:17:22Z")

</div>

I implemented beforeChannelJoin hook from [here](https://heroiclabs.com/docs/nakama/guides/server-framework/using-hooks/#before-channel-join)

 ![Screenshot from 2022-11-02 15-18-41](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/a/a1ce3fe00c8cc9c2e05e88beffa3109dfcadc576.png)

There are some redline(error) showing just under beforeChannelJoin parameter in register function.  
**initializer.registerRtBefore(“ChannelJoin”, beforeChannelJoin);**  
When I hover mouse on it, it shows

```auto
let beforeChannelJoin: nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeChannelJoin>
Argument of type 'RtBeforeHookFunction<EnvelopeChannelJoin>' is not assignable to parameter of type 'RtBeforeHookFunction<Envelope>'.
  Types of parameters 'envelope' and 'envelope' are incompatible.
    Type 'Envelope' is not assignable to type 'EnvelopeChannelJoin'.
      Type 'EnvelopeChannel' is not assignable to type 'EnvelopeChannelJoin'.ts(2345)

```

There are also red line(error) showing under **result.friends.filter(function (friend)** and **friend.user.userId**  
When I hover I get same error for both of them:

```auto
Object is possibly 'undefined'.ts(2532)

```

**main.ts**

```auto
let InitModule: nkruntime.InitModule = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  initializer: nkruntime.Initializer
) {
  initializer.registerRtBefore("ChannelJoin", beforeChannelJoin);
};
let beforeChannelJoin: nkruntime.RtBeforeHookFunction<nkruntime.EnvelopeChannelJoin> = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  envelope: nkruntime.EnvelopeChannelJoin
): nkruntime.EnvelopeChannelJoin | void {
  // If the channel join is a DirectMessage type, check to see if the user is friends with the recipient first
  if (envelope.channelJoin.type == nkruntime.ChanType.DirectMessage) {
    const result = nk.friendsList(ctx.userId, null, 0, null);
    const filtered = result.friends.filter(function (friend) {
      return friend.user.userId == envelope.channelJoin.target;
    });

    if (filtered.length == 0) {
      throw new Error(
        "You cannot direct message someone you are not friends with."
      );
    }
  }

  return envelope;
};

```
