Event Delegate

The iOS SDK provides three types of delegates for event handling: channel delegate, user event delegate, and connection delegate. Through the channel delegate and user event delegate, a 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 delegate.

Using the event callbacks provided by two types of delegates, you can write the event-related processing code you want to execute. For example, channel:didReceiveMessage: method of SBDChannelDelegate is called when a message has been received in a channel. This callback receives SBDBaseChannel and SBDBaseMessage objects which contain new information about the channel and message as parameters.

The SendBird SDK interacts 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 delegates. This allows you to track the events and implement your own chat features associated with them.


Add and remove a channel delegate

To receive information about certain events happening in the channels from SendBird server, you need to register a channel delegate (SBDChannelDelegate) with its unique user-defined ID. 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 delegates to each view controller.

Supported event callbacks for open channels

Method When to call Which device to notify (when a client app is in the foreground)
didReceiveMessage: A message has been received in an open channel. All devices running client apps with the channel, except the device that sent the message.
didUpdateMessage: A message has been updated in an open channel. All devices running client apps with the channel, except the device where the message was updated.
messageWasDeleted: A message has been deleted in an open channel. All devices running client apps with the channel, including the device where the message was deleted.
didReceiveMention: A user has been mentioned in a message sent in an open channel. All devices of mentioned users in the channel, including the device that was used to mention other users.
channelWasChanged: 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.
channelWasDeleted: An open channel has been deleted. All devices running client apps with the channel, including the device where the channel was deleted.
channelWasFrozen: An open channel has got frozen. All devices running client apps with the channel, including the device where the channel got frozen.
channelWasUnfrozen: An open channel has got unfrozen. All devices running client apps with the channel, including the device where the channel got unfrozen.
createdMetaData: A metadata for an open channel has been created. All devices running client apps with the channel, including the device where the metadata was created.
updatedMetaData: A metadata for an open channel has been updated. All devices running client apps with the channel, including the device where the metadata was updated.
deletedMetaDataKeys: A metadata for an open channel has been deleted. All devices running client apps with the channel, including the device where the metadata was deleted.
createdMetaCounters: A metacounter for an open channel has been created. All devices running client apps with the channel, including the device where the metacounter was created.
updatedMetaCounters: A metacounter for an open channel has been updated. All devices running client apps with the channel, including the device where the metacounter was updated.
deletedMetaCountersKeys: A metacounter for an open channel has been deleted. All devices running client apps with the channel, including the device where the metacounter was deleted.
userDidEnter: A user has entered an open channel. All devices running client apps with the channel, including the device that the user entered that channel.
userDidExit: A user has exited an open channel. All devices running client apps with the channel, except the device that the user exited that channel.
userWasMuted: A user has been muted in an open channel. All devices running client apps with the channel, including the device where the user was muted.
userWasUnmuted: A user has been unmuted in an open channel. All devices running client apps with the channel, including the device where the user was unmuted.
userWasBanned: A user has been banned from an open channel. All devices running client apps with the channel, including the device where the user was banned.
userWasUnbanned: A user has been unbanned from an open channel. All devices running client apps with the channel, including the device where the user was unbanned.

Supported event callbacks for group channels

Method When to call Which device to notify (when a client app is in the foreground)
didReceiveMessage: A message has been received in a group channel. All devices running client apps with the channel, except the device that sent the message.
didUpdateMessage: A message has been updated in a group channel. All devices running client apps with the channel, except the device where the message was updated.
messageWasDeleted: A message has been deleted in a group channel. All devices running client apps with the channel, including the device where the message was deleted.
didReceiveMention: A user has been mentioned in a message sent in a group channel. All devices of mentioned users (up to 10) in the channel, except the device that was used to mention other users.
channelWasChanged: One of the following group channel properties has been changed: Distinct, push notification preference, last message (except in case of 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.                                                  
channelWasDeleted: A group channel has been deleted. All devices running client apps with the channel, including the device where the channel was deleted.
channelWasFrozen: A group channel has got frozen. All devices running client apps with the channel, including the device where the channel got frozen.
channelWasUnfrozen: A group channel has got unfrozen. All devices running client apps with the channel, including the device where the channel got unfrozen.
createdMetaData: A metadata for a group channel has been created. All devices running client apps with the channel, including the device where the metadata was created.
updatedMetaData: A metadata for a group channel has been updated. All devices running client apps with the channel, including the device where the metadata was updated.
deletedMetaData: A metadata for a group channel has been deleted. All devices running client apps with the channel, including the device where the metadata was deleted.
channelWasHidden: A group channel has been hidden from the list.               All devices of a user who hided the channel.
didReceiveInvitation: A user has been invited to a group channel. All devices running client apps with the channel, including the device of a user who received an invitation.
didDeclineInvitation: A user has declined an invitaion to a group channel. All devices running client apps with the channel, including the device of a user who declined an invitation.
userDidJoin: A user has joined a group channel. All devices running client apps with the channel, including all devices of the user who joined the channel, and except the devices of users who haven't accepted invitations yet.
userDidLeave: A user has left a group channel. All devices running client apps with the channel, including all devices of the user who left the channel, and except the devices of users who haven't accepted invitations yet.                                                                
channelDidUpdateReadReceipt: A user has read a specific unread message in a group channel. All devices running client apps with the channel, including all devices of the users who are invited to the channel, and except the device of the user read the unread message.
channelDidUpdateTypingStatus: A user starts typing a message to a group channel. All devices running client apps with the channel, except the devices of the users who are invited to the channel, and except all devices of the user who typed a message.
userWasMuted: A user has been muted in a group channel. All devices running client apps with the channel, including the device where the user was muted.
userWasUnmuted: A user has been unmuted in a group channel. All devices running client apps with the channel, including the device where the user was unmuted.
userWasBanned: A user has been banned from a group channel. All devices running client apps with the channel, including the device where the user was banned.
userWasUnbanned: A user has been unbanned from a group channel. All devices running client apps with the channel, including the device where the user was unbanned.

The following code shows a full set of supported event callbacks with their parameters and how to register a channel delegate to the unique SendBird instance.

// ViewController.m
@interface ViewController : UIViewController<SBDConnectionDelegate, SBDUserEventDelegate, SBDChannelDelegate>

@end

@implementation ViewController

- (void)initViewController {
    // ...

    [SBDMain addChannelDelegate:self identifier:UNIQUE_DELEGATE_ID];

    // ...
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender didReceiveMessage:(SBDBaseMessage * _Nonnull)message {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender didUpdateMessage:(SBDBaseMessage * _Nonnull)message {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender messageWasDeleted:(long long)messageId {
}

- (void)channel:(nonnull SBDBaseChannel *)channel didReceiveMention:(nonnull SBDBaseMessage *)message {
}

- (void)channelWasChanged:(SBDBaseChannel * _Nonnull)sender {
}

- (void)channelWasDeleted:(NSString * _Nonnull)channelUrl channelType:(SBDChannelType)channelType {
}

- (void)channelWasFrozen:(SBDBaseChannel * _Nonnull)sender {
}

- (void)channelWasUnfrozen:(SBDBaseChannel * _Nonnull)sender {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender createdMetaData:(NSDictionary<NSString *, NSString *> * _Nullable)createdMetaData {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender updatedMetaData:(NSDictionary<NSString *, NSString *> * _Nullable)updatedMetaData {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender deletedMetaDataKeys:(NSArray<NSString *> * _Nullable)deletedMetaDataKeys {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender createdMetaCounters:(NSDictionary<NSString *, NSNumber *> * _Nullable)createdMetaCounters {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender updatedMetaCounters:(NSDictionary<NSString *, NSNumber *> * _Nullable)updatedMetaCounters {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender deletedMetaCountersKeys:(NSArray<NSString *> * _Nullable)deletedMetaCountersKeys {
}

- (void)channelWasHidden:(SBDGroupChannel * _Nonnull)sender {
}

- (void)channel:(SBDGroupChannel * _Nonnull)sender didReceiveInvitation:(NSArray<SBDUser *> * _Nullable)invitees inviter:(SBDUser * _Nullable)inviter {
}

- (void)channel:(SBDGroupChannel * _Nonnull)sender didDeclineInvitation:(SBDUser * _Nonnull)invitee inviter:(SBDUser * _Nullable)inviter {
}

- (void)channel:(SBDGroupChannel * _Nonnull)sender userDidJoin:(SBDUser * _Nonnull)user {
}

- (void)channel:(SBDGroupChannel * _Nonnull)sender userDidLeave:(SBDUser * _Nonnull)user {
}

- (void)channelDidUpdateReadReceipt:(SBDGroupChannel * _Nonnull)sender {
}

- (void)channelDidUpdateTypingStatus:(SBDGroupChannel * _Nonnull)sender {
}

- (void)channel:(SBDOpenChannel * _Nonnull)sender userDidEnter:(SBDUser * _Nonnull)user {
}

- (void)channel:(SBDOpenChannel * _Nonnull)sender userDidExit:(SBDUser * _Nonnull)user {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender userWasMuted:(SBDUser * _Nonnull)user {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender userWasUnmuted:(SBDUser * _Nonnull)user {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender userWasBanned:(SBDUser * _Nonnull)user {
}

- (void)channel:(SBDBaseChannel * _Nonnull)sender userWasUnbanned:(SBDUser * _Nonnull)user {
}

@end
// ViewController.swift
class ViewController: UIViewController, SBDConnectionDelegate, SBDUserEventDelegate, SBDChannelDelegate {
    func initViewController() {

        // ...
        SBDMain.add(self as SBDChannelDelegate, identifier: UNIQUE_DELEGATE_ID)

        // ...
    }

    func channel(_ sender: SBDBaseChannel, didReceive message: SBDBaseMessage) {
    }

    func channel(_ sender: SBDBaseChannel, didUpdate message: SBDBaseMessage) {
    }

    func channel(_ sender: SBDBaseChannel, messageWasDeleted messageId: Int64) {
    }

    func channel(_ channel: SBDBaseChannel, didReceiveMention message: SBDBaseMessage) {
    }

    func channelWasChanged(_ sender: SBDBaseChannel) {
    }

    func channelWasDeleted(_ channelUrl: String, channelType: SBDChannelType) {
    }

    func channelWasFrozen(_ sender: SBDBaseChannel) {
    }

    func channelWasUnfrozen(_ sender: SBDBaseChannel) {
    }

    func channel(_ sender: SBDBaseChannel, createdMetaData: [String : String]?) {
    }    

    func channel(_ sender: SBDBaseChannel, updatedMetaData: [String : String]?) {
    }

    func channel(_ sender: SBDBaseChannel, deletedMetaDataKeys: [String]?) {
    }    

    func channel(_ sender: SBDBaseChannel, createdMetaCounters: [String : NSNumber]?) {
    }

    func channel(_ sender: SBDBaseChannel, updatedMetaCounters: [String : NSNumber]?) {
    }

    func channel(_ sender: SBDBaseChannel, deletedMetaCountersKeys: [String]?) {
    }

    func channelWasHidden(_ sender: SBDGroupChannel) {
    }

    func channel(_ sender: SBDGroupChannel, didReceiveInvitation invitees: [SBDUser]?, inviter: SBDUser?) {
    }

    func channel(_ sender: SBDGroupChannel, didDeclineInvitation invitee: SBDUser?, inviter: SBDUser?) {
    }

    func channel(_ sender: SBDGroupChannel, userDidJoin user: SBDUser) {
    }

    func channel(_ sender: SBDGroupChannel, userDidLeave user: SBDUser) {
    }

    func channelDidUpdateReadReceipt(_ sender: SBDGroupChannel) {
    }

    func channelDidUpdateTypingStatus(_ sender: SBDGroupChannel) {
    }

    func channel(_ sender: SBDOpenChannel, userDidEnter user: SBDUser) {
    }

    func channel(_ sender: SBDOpenChannel, userDidExit user: SBDUser) {
        }

    func channel(_ sender: SBDBaseChannel, userWasMuted user: SBDUser) {
    }

    func channel(_ sender: SBDBaseChannel, userWasUnmuted user: SBDUser) {
    }

    func channel(_ sender: SBDBaseChannel, userWasBanned user: SBDUser) {
    }

    func channel(_ sender: SBDBaseChannel, userWasUnbanned user: SBDUser) {
    }
}

The following code shows how to remove the channel delegate.

[SBDMain removeChannelDelegateForIdentifier:UNIQUE_DELEGATE_ID];
SBDMain.removeChannelDelegate(forIdentifier: UNIQUE_DELEGATE_ID)

Add and remove a user event delegate

To receive information about events related to users connected to a SendBird server, you need to register a user event delegate (SBDUserEventDelegate) with its unique user-defined ID. If you want to keep informed of changes related to the users, define and register multiple user event delegates to each view controller.

Supported event callbacks

Method When to call Which device to notify (when a client app is in the foreground)
didDiscoverFriends: The discovery key uploaded by a user who is seeking friends has matched the corresponding key uploaded by one or more other users. All devices of the users with the corresponding unique identifiers and of the user that uploaded the discovery key.

The following code shows a full set of supported event callbacks with their parameters and how to register a user event delegate to the unique SendBird instance.

// ViewController.m
@interface ViewController : UIViewController<SBDConnectionDelegate, SBDUserEventDelegate, SBDChannelDelegate>

@end

@implementation ViewController

- (void)initViewController {

        // ...
        [SBDMain addUserEventDelegate:self identifier:UNIQUE_DELEGATE_ID];

        // ...
}

- (void)didDiscoverFriends:(NSArray<SBDUser *> * _Nullable)friends {
}
// ViewController.swift
class ViewController: UIViewController, SBDConnectionDelegate, SBDUserEventDelegate, SBDChannelDelegate {
        func initViewController() {

                // ...
                SBDMain.add(self as SBDUserEventDelegate, identifier: UNIQUE_DELEGATE_ID)

                // ...
        }

        func didDiscoverFriends(_ friends: [SBDUser]?) {
        }
}

The following code shows how to remove the user event delegate.

[SBDMain removeUserEventDelegateForIdentifier:UNIQUE_DELEGATE_ID];
SBDMain.removeUserEventDelegate(forIdentifier: UNIQUE_DELEGATE_ID)

Add and remove a connection delegate

To detect changes in the connection status of a client app, you need to register a connection delegate (SBDConnectionDelegate) with its unique user-defined ID. If you want to keep informed of changes related to the SendBird server connection, define and register multiple connection delegates to each view controller.

Method When to call Which device to notify (when a client app is in the foreground)
didStartReconnection SDK has started reconnecting to SendBird server. The device where connectWithUserId: was automatically called by the SDK, or manually by the client app.
didSucceedReconnection SDK has succeeded in reconnecting to SendBird server. The device that successfully reconnected to the server.
didFailReconnection SDK has failed to reconnect to SendBird server. The device that failed to reconnect to the server.
didCancelReconnection SDK has cancelled to reconnect to SendBird server. The device that cancelled to reconnect to the server.

The following code shows a full set of supported event callbacks and how to register a connection delegate to the unique SendBird instance.

// ViewController.m
@interface ViewController : UIViewController<SBDConnectionDelegate, SBDChannelDelegate>

@end

@implementation ViewController

- (void)initViewController {

    // ...
    [SBDMain addConnectionDelegate:self identifier:UNIQUE_DELEGATE_ID];

    // ...
}

- (void)didStartReconnection {
}

- (void)didSucceedReconnection {
}

- (void)didFailReconnection {
}

- (void)didCancelReconnection {
}
// ViewController.swift
class ViewController: UIViewController, SBDConnectionDelegate, SBDChannelDelegate {
    func initViewController() {

        // ...
        SBDMain.add(self as SBDConnectionDelegate, identifier: UNIQUE_DELEGATE_ID)

        // ...
    }

    func didStartReconnection() {
    }

    func didSucceedReconnection() {
    }

    func didFailReconnection() {
    }

    func didCancelReconnection() {
    }
}

The following code shows how to remove the connection delegate.

[SBDMain removeConnectionDelegateForIdentifier:UNIQUE_DELEGATE_ID];
SBDMain.removeConnectionDelegate(forIdentifier: UNIQUE_DELEGATE_ID)