Event Handler

Event Handlers are crucial components of the SendBird SDK that allow a client to react to server-side events. These handlers contain callback methods that can be overridden to respond to specific chat-related events passed from the server. For example, ChannelHandler.onMessageReceived(BaseChannel, BaseMessage) is triggered whenever a message is received. The specifics of each received message is contained within the BaseChannel and BaseMessage arguments passed in the triggering callback.

By providing its own Event Handlers, the SendBird SDK allows a client to respond to asynchronous events without worrying about the plethora of issues surrounding client-server communication and multithreading. A chat application especially involves rapid exchanges of data that must take place in near real-time across potentially thousands of users. Therefore, the SDK optimizes communication and threading to ensure data integrity between users and servers. Add Event Handlers and implement the necessary callback methods to track events occuring within channels or a user's own device.


Channel Handler

Register a ChannelHandler to receive information whenever events occur within a channel.

You can register multiple channel handlers. Each handler must have its unique identifier UNIQUE_HANDLER_ID. Typically, event handlers are registered in each activity to stay up to date with changes in the channel, as well as notify the channel of the user's own activity.

SendBirdClient.ChannelHandler ch = new SendBirdClient.ChannelHandler();

ch.OnMessageReceived = (BaseChannel baseChannel, BaseMessage baseMessage) => {
    // Received a chat message.
};

ch.OnMessageDeleted = (BaseChannel baseChannel, long messageId) => {
  // When a message has been deleted.
};

ch.OnChannelChanged = (BaseChannel baseChannel) => {
  // When a channel property has been changed.
};

ch.OnChannelDeleted = (string channelUrl, BaseChannel.ChannelType channelType) => {
  // When a channel has been deleted.
};

ch.OnReadReceiptUpdated = (GroupChannel groupChannel) => {
    // When read receipt has been updated.
};

ch.OnTypingStatusUpdated = (GroupChannel groupChannel) => {
    // When typing status has been updated.
};

ch.OnUserJoined = (GroupChannel groupChannel, User user) => {
  // When a new member joined the group channel.
};

ch.OnUserLeft(GroupChannel groupChannel, User user) => {
  // When a member left the group channel.
};

ch.OnUserEntered = (OpenChannel openChannel, User user) => {
  // When a new user entered the open channel.
};

ch.OnUserExited = (OpenChannel openChannel, User user) => {
  // When a new user left the open channel.
};

ch.OnUserMuted = (OpenChannel openChannel, User user) => {
  // When a user is muted on the open channel.
};

ch.OnUserUnmuted = (OpenChannel openChannel, User user) => {
  // When a user is unmuted on the open channel.
};

ch.OnUserBanned = (OpenChannel openChannel, User user) => {
  // When a user is banned on the open channel.
};

ch.OnUserUnbanned = (OpenChannel openChannel, User user) => {
  // When a user is unbanned on the open channel.
};

ch.OnChannelFrozen(OpenChannel openChannel) => {
  // When the open channel is frozen.
};

ch.OnChannelUnfrozen(OpenChannel openChannel) => {
  // When the open channel is unfrozen.
};


SendBirdClient.AddChannelHandler(UNIQUE_HANDLER_ID, ch);

OnChannelChanged() is called whenever a one of the following channel properties have been changed :

  • Push preference
  • Last message (except in cases where the message is a silent Admin message)
  • Unread message count
  • Name, cover image, data, custom type
  • Operators (only applicable to open channels)
  • Distinct property (only applicable to group channels)

Where the activity isn't valid anymore, remove the channel handler.

SendBirdClient.RemoveChannelHandler(UNIQUE_HANDLER_ID);

Connection Handler

Register a ConnectionHandler to detect changes in the user's own connection status.

You can register multiple connection handlers. Each handler has its unique identifier UNIQUE_HANDLER_ID. Typically, connection handlers are registered in each activity to monitor the state of the user's connection with the SendBird servers.

SendBirdClient.ConnectionHandler ch = new SendBirdClient.ConnectionHandler ();

ch.OnReconnectFailed = () => {
    // Auto reconnecting failed. Call `connect` to reconnect to SendBird.
};

ch.OnReconnectStarted = () => {
    // Network has been disconnected. Auto reconnecting starts.
};

ch.OnReconnectSucceeded = () => {
    // Auto reconnecting succeeded.
};

SendBirdClient.AddConnectionHandler(UNIQUE_HANDLER_ID, ch);

Where the activity isn't valid anymore, remove the connection handler.

SendBirdClient.RemoveConnectionHandler(UNIQUE_HANDLER_ID);