Chat SDKs Unity v3
Chat SDKs Unity
Chat SDKs
Unity
Version 3
Sendbird Chat SDK v3 for Unity is no longer supported as a new version is released. Check out our latest Chat SDK v4

Event handler

Copy link

Sendbird Chat SDK for Unity provides two types of event handlers: channel handler and connection handler. Through the channel handler, Sendbird server notifies client apps of events that happen on the channels and users of your app. In cases where the client app gets disconnected from the server and tries to reconnect at the SDK level, the server notifies the client app through the connection handler.

Using the event callbacks provided by two types of handlers, you can write the event-related processing code you want to execute. For example, the ChannelHandler.onMessageReceived() is called when a message has been received in a channel. This callback receives BaseChannel and BaseMessage objects which contain new information on the channel and message as parameters.

The Sendbird SDKs interact with our server through persistent WebSocket connections and multi-thread processing, and receives callbacks of asynchronous events which happen on the channels, users, and reconnect process of client apps in real-time through the handlers. This allows you to track the events and implement your own chat features associated with them.


Add and remove a channel handler

Copy link

To receive and retrieve information about certain events happening in the channels from Sendbird server, you need to register a channel handler with its unique user-defined ID by calling the SendBirdClient.AddChannelHandler(). If you want to keep informed of changes related to the channels and notify other users' client apps of those changes, define and register multiple channel handlers to each activity instance.

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 the 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);

The OnChannelChanged() method 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);

Add and remove a connection handler

Copy link

To detect changes in the connection status of a client app, you need to register a connection handler with its unique user-defined ID by calling the SendBirdClient.AddConnectionHandler(). If you want to keep informed of changes related to the Sendbird server connection, define and register multiple connection handlers to each activity instance.

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);