Subscribe to CometChat

The subscribe method allows CometChat to receive the data from the server.

Initially, follow the steps mentioned in Initialization section to obtain an instantiated object of the CometChat class. Then use the object to subscribe to CometChat.

Subscribe to CometChat

Subscribe to CometChat using subscribe() function to get the messages, userinfo etc.
The Syntax is as follows:

   
   - (void)SubscribeCallbacks:(BOOL)mode
             onMyInfoReceived:(void (^)(NSDictionary *response))myInfo
           onUserListReceived:(void (^)(NSDictionary *response))onlineUsers
            onMessageReceived:(void (^)(NSDictionary *response))oneOnOneMessage
      onAVChatMessageReceived:(void (^)(NSDictionary *response))oneOnOneAVChatMessage
      onActionMessageReceived:(void (^)(NSDictionary *response))oneOnOneactionMessage
          onGroupListReceived:(void (^)(NSDictionary *response))groupList
       onGroupMessageReceived:(void (^)(NSDictionary *response))groupMeessage
 onGroupAVChatMessageReceived:(void (^)(NSDictionary *response))groupAVChatMessage
 onGroupActionMessageReceived:(void (^)(NSDictionary *response))groupActionMessage
     onRecentChatListReceived:(void (^)(NSDictionary *response))recentList
       onAnnouncementReceived:(void (^)(NSDictionary *response))announcement
                      failure:(void (^)(NSError *))failure;

Arguments:

htmlFlag:

A boolean flag is set to true you will receive emoji tags(e.g :smiley: format) instead of the entire html content for emoji messages.

subscribeCallback:

The subscribeCallback allows you to override the functionality on receiving a data from the server.
The Available Callbacks:

Callbacks Invocation
onMyInfoReceived The callback triggers on receiving the logged-in users profile information
onUserListReceived The callback triggers on receiving contact list
onMessageReceived The callback triggers on receiving text chat message from a user
onAVChatMessageReceived The callback triggers on receiving Audio Video chat message from a user
onActionMessageReceived The callback triggers on receiving action message from a user
onGroupListReceived The callback triggers on receiving group list
onGroupMessageReceived The callback triggers on receiving text chat message from a user
onGroupAVChatMessageReceived The callback triggers on receiving Audio Video chat message from a user
onGroupActionMessageReceived The callback triggers on receiving action message from a user
onRecentChatListReceived The callback triggers on receiving recent chat list
onAnnouncementReceived The callback triggers on receiving an announcement from CometChat Admin
failure If there is any error while communicating with the server

onMyInfoReceived

Response:

{
    "id" : "5",
    "n" : "MyName",
    "l" : "link", 
    "a" : "avatar.png",
    "s" : "available",
    "m" : "I'm available",
    "push_channel" : "C_e9d4eb1e42bc17d6540e46a72af90c81",
    "push_an_channel" : "ANN_1c361dfb0ac82a9f988ed4c1f4bcb785"
}

Description:

Response Property Description
id Userid for the user
n Name of the user
l Profile page URL of the user
a Avatar/Profile picture of the user
s Status of the user
m Status message of the user
push_channel Users unique channel to receive mobile push notification
push_an_channel Channel to receive push notification for announcements added through CometChat Admin Panel

onUserListReceived

Response:

{
  {
    "id": "1", "n": "User1", "a": "avatar.png", "s": "available","m": "I'm available", "d": "1", "ls": 1498643206, "lstn":"0"
  },
  {
    "id": "2", "n": "User2", "a": "avatar.png", "s": "available","m": "I'm available", "d": "1", "ls": 1498643206, "lstn":"1"
  }
}

Description:

Response Property Description
id Userid for the user
n Name of the user
l Profile page URL of the user
a Avatar/Profile picture of the user
s Status of the user
m Status message of the user
d Platform From which user is using CometChat. “0” indicates web and “1” indicates mobile
ls last active time of the user as UNIX TIMESTAMP
lstn lastseen setting of the user. “0” indicates desabled and “1” indicates enabled

onMessageReceived

Response:

{
    message:{
        "id": "64",
        "message": "The Long Night is coming and the Dead come with it.",
        "self": 0,
        "old": "1",
        "sent": 1423645138, 
        "from": "73", 
        "message_type":"10"
    }
}

Description:

Response Property Description
id unique identifier for the message
message Text Message
self Indicates whether the loggeg-in user is sender or receiver. 0 indicates receiver, 1 indicates sender
old Indicates whether the message is new or old. 0 indicates new and 1 indicates old
sent The UNIX TIMESTAMP time at which the message was sent
from userid of the othe chat participant
message_type Numeric value representing type of the message

Available message types:

Type Description
10 A text/emoji as a message
11 Join group message, when user is invited to join a group
12 Image message. The image’s download link will be available in the message
13 Handwrite message. You will get download link of handwrite message’s image
14 Video message, the video’s download link will be available in message
16 Audio message, the audio’s download link will be available in message
17 Regular file like .txt, .pdf, the file’s download link will be available in message
18 Sticker message

onAVChatMessageReceived

Response:
Audio Chat:

{
    "id": 10678,
    "from": 1,
    "message": "AUDIOCHAT_INCOMING_CALL",
    "sent": 1448355885,
    "self": 0,
    "callid": "a1836634d06ab7b06374f03ec6623eb9",
    "message_type": 32,
    "pluginType": 0
}

Audio Video Chat:

{
    "id": 10678,
    "from": 1,
    "message": "AVCHAT_INCOMING_CALL",
    "sent": 1448355920,
    "self": 0,
    "callid": "a1836634d06ab7b06374f03ec6623eb9",
    "message_type": 32,
    "pluginType": 1
}

Audio Video Broadcast:

{
    "id": 10678,
    "from": 1,
    "message": "INCOMING_AVBROADCAST_REQUEST",
    "sent": 1453361688,
    "self": 0,
    "callid": "a1836634d06ab7b06374f03ec6623eb9",
    "message_type": 32,
    "pluginType": 2
}

Description:

Response Property Description
id unique identifier for the message
from userid of the othe chat participant
message Text Message
sent The UNIX TIMESTAMP time at which the message was sent
self Indicates whether the loggeg-in user is sender or receiver. 0 indicates receiver, 1 indicates sender
callid identifier of a call
message_type Numeric value representing type of the message
pligin_type 0 indicates audio chat, 1 indicates audio video chat and 2 indicates audio video broadcast

Available message types:

Type Description
31 Callee has accepted the Audio/Video chat request sent by you
32 Caller has sent an Audio/Video chat request
33 New incoming call while logged-in user is already engaged in other audio video session
34 Audio/Video chat call has ended
35 Callee has rejected the call
36 Caller has cancelled the call
37 Calle has left the call unanswered
38 Callee in busy in other audio video session
41 Incoming broadcast request
42 Broadcaster has ended the broadcast session
43 Logged-in user is invited to join a broadcast

onActionMessageReceived

Typing Notication

Response:
Typing Start:

{"action":"typing_start","from":"12","sent":"1450849776899"}

Typing Stop:

{"action":"typing_stop","from":"12","sent":"1450849780098"}

Description:

Response Property Description
action Indicates whether a user has started or stopped typing
from userid of the other chat participant
sent The UNIX TIMESTAMP time at which the typing notification was sent

Message Receipts

Response:
Message Delivered:

{"action":"message_deliverd","from":"12","message_id":"1201"}

Message Receipt:

{"action":"message_read","from":"12","message_id":"1201"}

Description:

Response Property Description
action Indicates whether the message has been delivered or read
from userid of the other chat participant
message_id id of a message

onAnnouncementReceived

Response:

{ 
  "id":"1",
  "m":"Announcements test",
  "t":"1416576743"
}

Description:

Response Property Description
id unique identifier for the announcement
m Announcement text
t The UNIX TIMESTAMP time at which the announcement was added

onGroupListReceived

Response:

{
    "_4":{
        "id":"4", "name":"group1","online":"1","type":"0", "i":"","s":0
    },
    "_2":{
        "id":"2","name":"group2","online":"0","type":"0","i":"","s":0
    }
}

Description:

Response Property Description
id id of the group
name Name of the group
online The number of the participants in the group
type Type of the group. 0 indicates public, 1 indicates password protected and 2 indicates invitation only groups
i represents password for password protected group
m Status message of the user
s 1 indicates Owner, 2 indicates Moderator and 0 Indicates logged-in user is neither Owner nor Moderator for the group

onGroupMessageReceived

Response:

{
    message:{
        "id": "64",
        "message": "Sometimes there is no happy choice, only one less griveous than the others",
        "self": 0,
        "old": "1",
        "sent": 1423645138, 
        "from": "Jon Snow", 
        "fromid":5,
        "message_type":"10"
    }
}

Description:

Response Property Description
id unique identifier for the message
message Text Message
self Indicates whether the loggeg-in user is sender or receiver. 0 indicates receiver, 1 indicates sender
old Indicates whether the message is new or old. 0 indicates new and 1 indicates old
sent The UNIX TIMESTAMP time at which the message was sent
from Name of the sender
fromid userid of the sender
message_type Numeric value representing type of the message

Available message types:

Type Description
10 A text/emoji as a message
12 Image message. The image’s download link will be available in the message
13 Handwrite message. You will get download link of handwrite message’s image
14 Video message, the video’s download link will be available in message
16 Audio message, the audio’s download link will be available in message
17 Regular file like .txt, .pdf, the file’s download link will be available in message
18 Sticker message

onGroupAVChatMessageReceived

Response:

{
    "id": "478",
    "message": "has started a video conversation.", 
    "self": 0,
    "old": 0, 
    "sent": "1422594713", 
    "from": "15", 
    "message_type": "32"
}
Response Property Description
id unique identifier for the message
message Text Message
self Indicates whether the loggeg-in user is sender or receiver. 0 indicates receiver, 1 indicates sender
old Indicates whether the message is new or old. 0 indicates new and 1 indicates old
sent The UNIX TIMESTAMP time at which the message was sent
from userid of the sender
message_type Numeric value representing type of the message

onGroupActionMessageReceived

Response:
Kick/Ban/Delete Group

{ 
    "action_type":12,
    "chatroom_id":"17"
}

Delete Message

{ 
    "action_type":13,
    "message_id":"123"
}

Available Actions types:

Action type Description
10 logged-in user has been kicked from the group
11 logged-in user has been banned from the group
12 The group has been deleted
13 The message has been deleted

onRecentChatListReceived

Response:

{
  {
    "id": "1", "n": "User1", "a": "avatar.png", "s": "available","m": "I'm available", "d": "1", "ls": 1498643206, "lstn":"0"
  },
  {
    "id": "2", "n": "User2", "a": "avatar.png", "s": "available","m": "I'm available", "d": "1", "ls": 1498643206, "lstn":"1"
  }
}

Description:

Response Property Description
id Userid for the user
n Name of the user
l Profile page URL of the user
a Avatar/Profile picture of the user
s Status of the user
m Status message of the user
d Platform From which user is using CometChat. “0” indicates web and “1” indicates mobile
ls last active time of the user as UNIX TIMESTAMP
lstn lastseen setting of the user. “0” indicates desabled and “1” indicates enabled

failure

Response:

{ 
    code: 1,
    message: "Error msg"
}

To get the various error codes, refer the ‎error codes

e.g


    [cometchat SubscribeCallbacks:YES onMyInfoReceived:^(NSDictionary *response) {
        /* Code Block */
    } onUserListReceived:^(NSDictionary *response) {
        /* Code Block */
    } onMessageReceived:^(NSDictionary *response) {
        /* Code Block */
    } onAVChatMessageReceived:^(NSDictionary *response) {
        /* Code Block */
    } onActionMessageReceived:^(NSDictionary *response) {
        /* Code Block */
    } onGroupListReceived:^(NSDictionary *response) {
        /* Code Block */
    } onGroupMessageReceived:^(NSDictionary *response) {
        /* Code Block */
    } onGroupAVChatMessageReceived:^(NSDictionary *response) {
        /* Code Block */
    } onGroupActionMessageReceived:^(NSDictionary *response) {
        /* Code Block */
    } onRecentChatListReceived:^(NSDictionary *response) {
        /* Code Block */
    } onBotListReceived:^(NSDictionary *response) {
        /* Code Block */
    } onAnnouncementReceived:^(NSDictionary *response) {
        /* Code Block */
    } failure:^(NSError *) {
        /* Code Block */
    }];
  • Was this helpful?
  • Yes   No