Chat / JavaScript
Chat JavaScript v4
Chat JavaScript
Chat
JavaScript
Home
/
Chat
/
JavaScript
/
Event handler
This is the new Docs for Chat SDK v4 for JavaScript. To see the previous Docs, click here.

Add or remove a channel event handler

To receive and retrieve information about events happening in channels from the Sendbird server, add a channel event handler with its UNIQUE_HANDLER_ID by calling sb.groupChannel.addGroupChannelHandler() for group channel events and sb.openChannel.addOpenChannelHandler() for open channel events.

If you want to stay informed of changes related to channels and notify the changes to different client apps, define and register multiple channel event handlers using UNIQUE_HANDLER_ID.


Channel event types

List of open channel events

MethodInvoked whenNotified devices

onMessageReceived()

A message has been received in an open channel.

All devices where client apps with the channel are in the foreground, except the device that sent the message.

onMessageUpdated()

A message has been updated in an open channel.

All devices where client apps with the channel are in the foreground, except the device where the message was updated.

onMessageDeleted()

A message has been deleted in an open channel.

All devices where client apps with the channel are in the foreground, including the device where the message was deleted.

onMentionReceived()

A user has been mentioned in a message sent in an open channel.

All devices of up to ten users mentioned in the channel, including the device that was used to mention other users.

onChannelChanged()

One of the following open channel properties has been changed: name, cover image, data, custom type, or operators.

All devices that are connected to the changed channel, including the device where the channel was changed.

onChannelDeleted()

An open channel has been deleted.

All devices where client apps with the channel are in the foreground, including the device where the channel was deleted.

onChannelFrozen()

An open channel has been frozen.

All devices where client apps with the channel are in the foreground, including the device where the channel was frozen.

onChannelUnfrozen()

An open channel has been unfrozen.

All devices where client apps with the channel are in the foreground, including the device where the channel was unfrozen.

onMetaDataCreated()

Metadata for an open channel has been created.

All devices where client apps with the channel are in the foreground, including the device where the metadata was created.

onMetaDataUpdated()

Metadata for an open channel has been updated.

All devices where client apps with the channel are in the foreground, including the device where the metadata was updated.

onMetaDataDeleted()

Metadata for an open channel has been deleted.

All devices where client apps with the channel are in the foreground, including the device where the metadata was deleted.

onMetaCounterCreated()

A metacounter for an open channel has been created.

All devices where client apps with the channel are in the foreground, including the device where the metacounter was created.

onMetaCounterUpdated()

A metacounter for an open channel has been updated.

All devices where client apps with the channel are in the foreground, including the device where the metacounter was updated.

onMetaCounterDeleted()

A metacounter for an open channel has been deleted.

All devices where client apps with the channel are in the foreground, including the device where the metacounter was deleted.

onUserEntered()

A user has entered an open channel.

All devices where client apps with the channel are in the foreground, including the device of the user who entered the channel.

onUserExited()

A user has exited an open channel.

All devices where client apps with the channel are in the foreground, except the device of the user who exited the channel.

onUserMuted()

A user has been muted in an open channel.

All devices where client apps with the channel are in the foreground, including the device where the user was muted.

onUserUnmuted()

A user has been unmuted in an open channel.

All devices where client apps with the channel are in the foreground, including the device where the user was unmuted.

onUserBanned()

A user has been banned from an open channel.

All devices where client apps with the channel are in the foreground, including the device where the user was banned.

onUserUnbanned()

A user has been unbanned from an open channel.

All devices where client apps with the channel are in the foreground, including the device where the user was unbanned.

onChannelParticipantCountChanged()

The participant count of one or more open channels has changed.

All connected devices where client apps have the affected open channels in their channel list.

List of group channel events

MethodInvoked whenNotified devices

onMessageReceived()

A message has been received in a group channel.

All devices where client apps with the channel are in the foreground, except the device that sent the message.

onMessageUpdated()

A message has been updated in a group channel.

All devices where client apps with the channel are in the foreground, except the device where the message was updated.

onMessageDeleted()

A message has been deleted in a group channel.

All devices where client apps with the channel are in the foreground, including the device where the message was deleted.

onMentionReceived()

A user has been mentioned in a message sent in a group channel.

All devices of up to ten users mentioned in the channel, except the device that was used to mention other users.

onReactionUpdated()

A message reaction has been updated in a group channel.

All devices where client apps with the channel are in the foreground, including the device that reacted to the message.

onChannelChanged()

One of the following group channel properties has been changed: distinct, push notification preferences, last message (except when the silent admin message), unread message count, name, cover image, data, or custom type.

All devices that are connected to the changed channel, including the device where the channel was changed.

onChannelDeleted()

A group channel has been deleted.

All devices where client apps with the channel are in the foreground, including the device where the channel was deleted.

onChannelFrozen()

A group channel has been frozen.

All devices where client apps with the channel are in the foreground, including the device where the channel was frozen.

onChannelUnfrozen()

A group channel has been unfrozen.

All devices where client apps with the channel are in the foreground, including the device where the channel was unfrozen.

onMetaDataCreated()

Metadata for a group channel has been created.

All devices where client apps with the channel are in the foreground, including the device where the metadata was created.

onMetaDataUpdated()

Metadata for a group channel has been updated.

All devices where client apps with the channel are in the foreground, including the device where the metadata was updated.

onMetaDataDeleted()

Metadata for a group channel has been deleted.

All devices where client apps with the channel are in the foreground, including the device where the metadata was deleted.

onMetaCounterCreated()

A metacounter for a group channel has been created.

All devices where client apps with the channel are in the foreground, including the device where the metacounter was created.

onMetaCounterUpdated()

A metacounter for a group channel has been updated.

All devices where client apps with the channel are in the foreground, including the device where the metacounter was updated.

onMetaCounterDeleted()

A metacounter for a group channel has been deleted.

All devices where client apps with the channel are in the foreground, including the device where the metacounter was deleted.

onChannelHidden()

A group channel has been hidden from the list.

All devices of a user who hid the channel.

onUserReceivedInvitation()

A user has been invited to a group channel.

All devices where client apps with the channel are in the foreground, including the devices of a user who received an invitation.

onUserDeclinedInvitation()

A user has declined an invitation to a group channel.

All devices where client apps with the channel are in the foreground, including the device of a user who declined an invitation.

onUserJoined()

A user has joined a group channel.

All devices where client apps with the channel are in the foreground, including all devices of the user who joined the channel, and except the devices of users who haven't accepted invitations.

onUserLeft()

A user has left a group channel.

All devices where client apps with the channel are in the foreground, including all devices of the user who left the channel, and except the devices of users who haven't accepted invitations.

onUndeliveredMemberStatusUpdated()

A message has been delivered in a group channel.

All devices where client apps with the channel are in the foreground, except the device where the message was marked as delivered and the device that invoked this event.

onUnreadMemberStatusUpdated()

A user has read a specific unread message in a group channel.

All devices where client apps with the channel are in the foreground, including all devices of the users who are invited to the channel, and except the device of the user who read the unread message.

onTypingStatusUpdated()

A user starts typing a message to a group channel.

All devices where client apps with the channel are in the foreground, except the devices of the users who are invited to the channel, and except the device of the user who typed the message.

onThreadInfoUpdated()

A reply message has been created or deleted from a thread.

All devices where client apps with the channel are in the foreground, including devices of all users who are in the message thread, and except the device of the user who created or deleted the reply message.

onUserMuted()

A user has been muted in a group channel.

All devices where client apps with the channel are in the foreground, including the device where the user was muted.

onUserUnmuted()

A user has been unmuted in a group channel.

All devices where client apps with the channel are in the foreground, including the device where the user was unmuted.

onUserBanned()

A user has been banned from a group channel.

All devices where client apps with the channel are in the foreground, including the device where the user was banned.

onUserUnbanned()

A user has been unbanned from a group channel.

All devices where client apps with the channel are in the foreground, including the device where the user was unbanned.

onChannelMemberCountChanged()

The member count of one or more group channels has changed.

All connected devices where client apps have the affected group channels in their channel list.


Add a channel event handler

The following code shows a full set of supported event callbacks with their parameters and how to add a channel event handler to the unique SendbirdChat instance.

const openChannelHandler: OpenChannelHandler = new OpenChannelHandler({
  onMessageReceived: (channel: BaseChannel, message: BaseMessage) => {},
  onMessageUpdated: (channel: BaseChannel, message: BaseMessage) => {},
  onMessageDeleted: (channel: BaseChannel, messageId: number) => {},
  onChannelChanged: (channel: BaseChannel) => {},
  onChannelDeleted: (channelUrl: string, channelType: ChannelType) => {},
  onChannelFrozen: (channel: BaseChannel) => {},
  onChannelUnfrozen: (channel: BaseChannel) => {},
  onMetaDataCreated: (channel: BaseChannel, metaData: MetaData) => {},
  onMetaCountersUpdated: (channel: BaseChannel, metaData: MetaData) => {},
  onMetaCountersDeleted: (channel: BaseChannel, metaDataKeys: string[]) => {},
  onChannelHidden: (channel: BaseChannel) => {},
  onUserEntered: (channel: OpenChannel, user: User) => {},
  onUserExited: (channel: OpenChannel, user: User) => {},
  onUserMuted: (channel: BaseChannel, user: User) => {},
  onUserUnmuted: (channel: BaseChannel, user: User) => {},
  onUserBanned: (channel: BaseChannel, user: User) => {},
  onUserUnbanned: (channel: BaseChannel, user: User) => {},
  onChannelParticipantCountChanged: (channels: OpenChannel[]) => {},
});

sb.openChannel.addOpenChannelHandler(UNIQUE_HANDLER_ID, openChannelHandler);

const groupChannelHandler: GroupChannelHandler = new GroupChannelHandler({
  onMessageReceived: (channel: BaseChannel, message: BaseMessage) => {},
  onMessageUpdated: (channel: BaseChannel, message: BaseMessage) => {},
  onMessageDeleted: (channel: BaseChannel, messageId: number) => {},
  onChannelChanged: (channel: BaseChannel) => {},
  onChannelDeleted: (channelUrl: string, channelType: ChannelType) => {},
  onChannelFrozen: (channel: BaseChannel) => {},
  onChannelUnfrozen: (channel: BaseChannel) => {},
  onMetaDataCreated: (channel: BaseChannel, metaData: MetaData) => {},
  onMetaCountersUpdated: (channel: BaseChannel, metaData: MetaData) => {},
  onMetaCountersDeleted: (channel: BaseChannel, metaDataKeys: string[]) => {},
  onChannelHidden: (channel: BaseChannel) => {},
  onUserReceivedInvitation: (channel: GroupChannel, inviter: User, invitees: User[]) => {},
  onUserDeclinedInvitation: (channel: GroupChannel, inviter: User, invitee: User) => {},
  onUserJoined: (channel: GroupChannel, user: User) => {},
  onUserLeft: (channel: GroupChannel, user: User) => {},
  onUndeliveredMemberStatusUpdated: (channel: GroupChannel) => {},
  onUnreadMemberStatusUpdated: (channel: GroupChannel) => {},
  onTypingStatusUpdated: (channel: GroupChannel) => {},
  onUserMuted: (channel: BaseChannel, user: User) => {},
  onUserUnmuted: (channel: BaseChannel, user: User) => {},
  onUserBanned: (channel: BaseChannel, user: User) => {},
  onUserUnbanned: (channel: BaseChannel, user: User) => {},
  onChannelMemberCountChanged: (channels: GroupChannel[]) => {},
});

sb.groupChannel.addGroupChannelHandler(UNIQUE_HANDLER_ID, groupChannelHandler);

Remove a channel event handler

The following code shows how to remove the channel event handler.

sb.groupChannel.removeGroupChannelHandler(UNIQUE_HANDLER_ID);
sb.openChannel.removeOpenChannelHandler(UNIQUE_HANDLER_ID);