All Real-Time Delegates (Listeners)

CometChat provides 4 listeners (Delegates) viz.

  1. User Delegate (Listener)
  2. Group Delegate (Listener)
  3. Message Delegate (Listener)
  4. Call Delegate (Listener)

User Delegate Methods

The CometChat provides you with live events related to users. Below are the callback methods provided by the CometChatUserDelegate.

Delegate Method

Information

onUserOnline:

This method is triggered when a user comes online and is available to chat. The details of the user can be obtained from the user object received as the method parameter.

onUserOffline:

This method is triggered when a user goes offline. The details of the user can be obtained from the User object received as the parameter.

In order to use the Delegate methods you must add protocol conformance CometChatUserDelegate as Shown Below:

extension ViewController : CometChatUserDelegate {
    
    func onUserOnline(user: User) {
        
        print(user.stringValue() + " status becomes online.")
    }
    
    func onUserOffline(user: User) {
        
        print(user.stringValue() + " status becomes offline.")
    }
}
@interface ViewController ()<CometChatUserDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CometChat setUserdelegate:self];
}

- (void)onUserOfflineWithUser:(User * _Nonnull)user {
    
    NSLog(@"%@ status becomes offline.",[user stringValue]);
}

- (void)onUserOnlineWithUser:(User * _Nonnull)user {
    
    NSLog(@"%@ status becomes online.",[user stringValue]);
}

❗️

Important

Do not forget to set your view controller as a CometChat delegate probably in viewDidLoad() as CometChat.userdelegate = self


Group Delegate Methods

The CometChatGroupDelegate provides you with all the real-time events related to groups. Below are the callback methods provided by the CometChatGroupDelegate.

Method

Information

onGroupMemberJoined(Action action, User joinedUser, Group joinedGroup)

This method is triggered when a user joins any group. All the members present in the group will receive this event.

onGroupMemberLeft(Action action, User leftUser, Group leftGroup)

This method is triggered when a user who was a member of any group leaves the group. All the members of the group receive this event.

onGroupMemberKicked(Action action, User kickedUser, User kickedBy, Group kickedFrom)

This method is triggered when a user is kicked from a group. All the members including the user receive this event

onGroupMemberBanned(Action action, User bannedUser, User bannedBy, Group bannedFrom)

This method is triggered when a user is banned from a group. All the members including the user receive this event

onGroupMemberUnbanned(Action action, User unbannedUser, User unbannedBy, Group unbannedFrom)

This method is triggered when a user is banned from a group. All the members of the group receive this event.

onGroupMemberScopeChanged(Action action, User updatedBy, User updatedUser, String scopeChangedTo, String scopeChangedFrom, Group group)

This method is triggered when the scope of any Group Member has been changed. All the members that are a part of that group receive this event

onMemberAddedToGroup(Action action, User addedby, User userAdded, Group addedTo)

This method is triggered when a user is added to any group. All the members including the user himself receive this event.

You can receive live events related to groups, In order to receive user Events, you must add protocol conformance CometChatGroupDelegate as shown below:

extension AppDelegate: CometChatGroupDelegate {

  func onGroupMemberJoined(action: ActionMessage, joinedUser: User, joinedGroup: Group) {

    print("\(joinedUser.name) joined the group \(joinedGroup.name).")
  }

  func onGroupMemberLeft(action: ActionMessage, leftUser: User, leftGroup: Group) {

    print("\(leftUser.name) left the group \(leftGroup.name).")
  }

  func onGroupMemberKicked(action: ActionMessage, kickedUser: User, kickedBy: User, kickedFrom: Group) {

    print("\(kickedUser.name) kicked from the group \(kickedFrom.name) by \(kickedBy.name).")
  }

  func onGroupMemberBanned(action: ActionMessage, bannedUser: User, bannedBy: User, bannedFrom: Group) {

    print("\(bannedUser.name) banned from the group \(bannedFrom.name) by \(bannedBy.name).")
  }

  func onGroupMemberUnbanned(action: ActionMessage, unbannedUser: User, unbannedBy: User, unbannedFrom: Group) {

    print("\(unbannedUser.name) unbanned from the group \(unbannedFrom.name) by \(unbannedBy.name).")
  }

  func onGroupMemberScopeChanged(action: ActionMessage, scopeChangeduser: User, scopeChangedBy: User, scopeChangedTo: String, scopeChangedFrom: String, group: Group) {
    
    print("\(scopeChangedUser.name) scope changed from \(scopeChangedFrom) to \(scopeChangedTo) by \(scopeChangedBy.name) in the group \(group.name).")
  }
}
@interface ViewController ()<CometChatGroupDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CometChat setGroupdelegate:self];
    
}
- (void)onGroupMemberBannedWithAction:(Action *)action bannedUser:(User * _Nonnull)bannedUser bannedBy:(User * _Nonnull)bannedBy bannedFrom:(Group * _Nonnull)bannedFrom {
    // user was unbanned
}

- (void)onGroupMemberJoinedWithAction:(Action *)action joinedUser:(User * _Nonnull)joinedUser joinedGroup:(Group * _Nonnull)joinedGroup {
    // user joined group
}

- (void)onGroupMemberKickedWithAction:(Action *)action kickedUser:(User * _Nonnull)kickedUser kickedBy:(User * _Nonnull)kickedBy kickedFrom:(Group * _Nonnull)kickedFrom {
    // user was kicked
}

- (void)onGroupMemberLeftWithAction:(ActionMessage *)action leftUser:(User *)leftUser leftGroup:(Group *)leftGroup {
    //user was left
}

- (void)onGroupMemberScopeChangedWithAction:(Action *)action user:(User * _Nonnull)user scopeChangedTo:(NSString * _Nonnull)scopeChangedTo scopeChangedFrom:(NSString * _Nonnull)scopeChangedFrom group:(Group * _Nonnull)group {
    // user scope changed
}

- (void)onGroupMemberUnbannedWithAction:(Action *)action unbannedUser:(User * _Nonnull)unbannedUser unbannedBy:(User * _Nonnull)unbannedBy unbannedFrom:(Group * _Nonnull)unbannedFrom {
    // user was unbanned
}

@end

❗️

Important

Do not forget to set your view controller as a CometChat delegate probably in viewDidLoad() as CometChat.groupdelegate = self


Message Delegate Methods

The CometChatMessageDelegate provides you with live events related to messages. Below are the callback methods provided by the CometChatMessageDelegate.

Method

Information

onTextMessageReceived(TextMessage message)

This event is triggered when a Text Message is received.

onMediaMessageReceived(MediaMessage message)

This event is triggered when a Media Message is received.

onCustomMessageReceived(CustomMessage message)

This event is triggered when a Custom Message is received.

onTypingStarted(TypingIndicator typingIndicator)

This event is triggered when a user starts typing in a user/group conversation

onTypingEnded(TypingIndicator typingIndicator)

This event is triggered when a user stops typing in a user/group conversation.

onMessagesDelivered(MessageReceipt messageReceipt)

This event is triggered when a set of messages are marked as delivered for any particular conversation.

onMessagesRead(MessageReceipt messageReceipt)

This event is triggered when a set of messages are marked as read for any particular conversation.

onMessageEdited(BaseMessage message)

This method is triggered when a particular message has been edited in a user/group conversation.

onMessageDeleted(BaseMessage message)

This event is triggered when a particular message is deleted in a user/group conversation.

In order to receive incoming messages, you must add protocol conformance CometChatMessageDelegate as Shown Below :

extension ViewController: CometChatMessageDelegate {

  func onTextMessageReceived(textMessage: TextMessage) {

    print("TextMessage received successfully: " + textMessage.stringValue())
  }

  func onMediaMessageReceived(mediaMessage: MediaMessage) {

    print("MediaMessage received successfully: " + mediaMessage.stringValue())
  }
  
  func onCustomMessageReceived(customMessage: CustomMessage) {

    print("CustomMessage received successfully: " + customMessage.stringValue())
  }
  
  func onMessageEdited(message: BaseMessage) {
        
        print("received edited message successfully.")
    }
  func onMessageDeleted(message: BaseMessage) {
        
        print("received deleted message successfully.")
    }
  func onTypingStarted(_ typingDetails : TypingIndicator) {

    print("Typing started received successfully")
  }

  func onTypingEnded(_ typingDetails : TypingIndicator) {

    print("Typing ended received successfully")
  }
  func onMessageDelivered(receipt : MessageReceipt) {
       
        print("Message delivered receipt received.")
    }
    
    func onMessageRead(receipt : MessageReceipt) {
        
        print("Message read receipt received.")
    }
}

❗️

Important

Do not forget to set your view controller as a CometChat delegate probably in viewDidLoad() as CometChat.messagedelegate = self


Call Delegate Methods

The CometChatCallDelegate provides you with live events related to calls. Below are the callback methods provided by the CometChatCallDelegate.

Method

Information

onIncomingCallReceived(Call call)

This event is triggered when the logged-in user receives an incoming call. The details of the call can be obtained from the Call object received as the method parameter.

onOutgoingCallAccepted(Call call)

This event is triggered when the call initiated by the logged-in user is accepted by the recipient. The details of the call can be obtained from the Call object received as the method parameter.

onOutgoingCallRejected(Call call)

This event is triggered when the call initiated by the logged-in user is rejected by the recipient. The details of the call can be obtained from the Call object received as the method parameter

onIncomingCallCancelled(Call call)

This event is triggered when an incoming call is canceled by the initiator of the call. The details of the call can be obtained from the Call object received as the method parameter

In order to receive all call events, you must add protocol conformance CometChatCallDelegate as Shown Below :

extension ViewController: CometChatCallDelegate {

  func onIncomingCallReceived(incomingCall: Call ? , error : CometChatException ? ) {
    print(" Incoming call " + incomingCall!.stringValue());
  }

  func onOutgoingCallAccepted(acceptedCall: Call ? , error : CometChatException ? ) {
    print("Outgoing call " + acceptedCall!.stringValue());
  }

  func onOutgoingCallRejected(rejectedCall: Call ? , error : CometChatException ? ) {
    print("Rejected call " + rejectedCall!.stringValue());
  }
  func onIncomingCallCanceled(canceledCall: Call ? , error : CometChatException ? ) {
    print("Cancelled call " + canceledCall!.stringValue());
  }
}
@interface ViewController ()<CometChatCallDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CometChat setCalldelegate:self];
}

- (void)onIncomingCallCanceledWithCanceledCall:(Call * _Nullable)canceledCall error:(CometChatException * _Nullable)error {
    
    NSLog(@"Incoming call cancelled %@",[canceledCall stringValue]);
}

- (void)onIncomingCallReceivedWithIncomingCall:(Call * _Nullable)incomingCall error:(CometChatException * _Nullable)error {
    
    NSLog(@"Incoming call %@",[incomingCall stringValue]);
}

- (void)onOutgoingCallAcceptedWithAcceptedCall:(Call * _Nullable)acceptedCall error:(CometChatException * _Nullable)error {
    
    NSLog(@"Outgoing call accepted %@",[acceptedCall stringValue]);
}

- (void)onOutgoingCallRejectedWithRejectedCall:(Call * _Nullable)rejectedCall error:(CometChatException * _Nullable)error {
    
    NSLog(@"Outgoing call rejected %@",[rejectedCall stringValue]);
}

@end

❗️

Important

Do not forget to set your view controller as a CometChat delegate probably in viewDidLoad() as CometChat.calldelegate = self



Did this page help you?