Chatrooms

To add Chatrooms support in your application,

Include the class in the <MessageSDKFramework/CometChatChatroom.h> file of your class.

All the method calls will invoke their respective callback block methods as defined below.

1. Initialize the CometChatChatroom object as follows

CometChatChatroom *cometchatChatroom =  [[CometChatChatroom alloc] init];

2. Subscribe to CometChat Chatrooms
Start receiving chatroom messages, chatroom list and chatroom members. mode signifies receiving messages with emoji support.

- (void)subscribeToChatroomWithMode:(BOOL)mode
          onChatroomMessageReceived:(void(^)(NSDictionary *response))message
            onActionMessageReceived:(void(^)(NSDictionary *response))actionMessage
            onChatroomsListReceived:(void(^)(NSDictionary *response))chatroomList
            onAVChatMessageReceived:(void(^)(NSDictionary *response))avchatMessage
                            failure:(void(^)(NSError *error))failure;

Return type: void
Callback Blocks:
(i). (void(^)(NSDictionary *response))message
Invocation: After receiving any type of message in chatroom.
Response: NSDictionary containing messages and its details.

Field “message_type” indicates the type of received message.
Types of messages received in this callback:

message_type = 10 : For normal text messages
message_type = 12 : For image messages
message_type = 13 : For handwritten messages
message_type = 14 : For video messages
message_type = 17 : For audio messages
message_type = 18 : For file messages
message_type = 24 : For bot responses

Eg:
Text message

{
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message" = "Hello";
    "sent" = 1415082627474;
    "message_type" = 10;
}

Image message

{
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message"="http://yoursite.com/cometchat/plugins/filetransfer/download.phpfile=a1d28da22369e330fab64e0250226
                d0b.png&unencryptedfilename=Image.png";
    "sent"= 1411035449792;
    "message_type" = 12;
}

Handwritten message

{
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message"="http://yoursite.com/cometchat/plugins/handwrite/uploads/9384627441b49aad5ef6bace29cdcd36.png";
    "sent"= 1411035449792;
    "message_type" = 13;
}

Video message

{
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message"="http://yoursite.com/cometchat/plugins/filetransfer/download.php?file=a1d28da22369e330fab64e0250226d
                0b.pngunencryptedfilename=Video.mp4";
    "sent"= 1411035449792;
    "message_type" = 14;
}

Audio message

{
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message"="http://yoursite.com/cometchat/plugins/filetransfer/download.phpfile=a1d28da22369e330fab64e0250226
                d0b.png&unencryptedfilename=sample.mp3";
    "sent"= 1411035449792;
    "message_type" = 17;
}

File message

{
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message"="http://yoursite.com/cometchat/plugins/filetransfer/download.phpfile=a1d28da22369e330fab64e0250226
                d0b.png&unencryptedfilename=samplefile.txt";
    "sent"= 1411035449792;
    "message_type" = 18;
}

Bot response

Bot response : Text
{
    "bot_id" = 32;
    "bot_response_type" = text;
    from = Me;
    fromid = 100;
    id = 1102;
    message = "You said test";
    "message_type" = 24;
    roomid = 34;
    sent = 1482406766;
}
Bot response : Image
{
    "bot_id" = 30;
    "bot_response_type" = image;
    from = Me;
    fromid = 100;
    id = 1104;
    message = "http://i.giphy.com/l2YWjLlllAFOkog9i.gif";
    "message_type" = 24;
    roomid = 34;
    sent = 1482406818;
}
Bot response : Anchor
{
    "bot_id" = 33;
    "bot_response_type" = anchor;
    from = Me;
    fromid = 100;
    id = 1106;
    message = "hi this is a test message!<br><a href=\"https://app.bots.co/components/add/1164\">bots</a>";
    "message_type" = 24;
    roomid = 34;
    sent = 1482406911;
}

(ii). (void(^)(NSDictionary *response))actionMessage
Invocation: Triggered when user gets action messages of chatroom. Action messages can be distinguished with key ‘action_type’. Types of actions:

KICK_ACTION
action_type = 10 : logged-in user is kicked from chatroom

BAN_ACTION
action_type = 11 : logged-in user is banned from chatroom

DELETE_CHATROOM_ACTION
action_type = 12 : Currently joined chatroom of the user is deleted.

DELETE_CHATROOM_MESSAGE_ACTION
action_type = 13 : Message from currently joined chatroom is deleted.

Response : NSDictionary containing action type and chatroom id. Incase of DELETE_CHATROOM_MESSAGE_ACTION, message id is received instead of chatroom id.

Eg:

KICK_ACTION
 {
   "action_type"="10";
   "chatroom_id"="5";
 }

BAN_ACTION
 {
   "action_type"="11";
   "chatroom_id"="5";
 }

DELETE_CHATROOM_ACTION
 {
   "action_type"="12";
   "chatroom_id"="5";
 }

DELETE_CHATROOM_MESSAGE_ACTION
 {
   "action_type"="10";
   "message_id"="45";
 }

(iii) (void(^)(NSDictionary *response))chatroomList
Invocation: Triggered when we get the complete chatroom list from the server.
Response: NSDictionary containing the list of all chatroom and their details.
Eg:

{
    "_4" = {
        "id" = "4";
        "name" = "room1";
        "online" = "1",
        "type" = "0";
        "i" = "";
        "s" = 0;
    },
    "_2" = {
        "id" = "2";
        "name" = "room2";
        "online" = "0";
        "type" = "0";
        "i" = "";
        "s" = 0;
    }
}

(iv). (void(^)(NSDictionary *response))avchatMessage
Invocation: When an audio video conference message is received.
Response: NSDictionary containing audio video group conference messages and its details NSInteger indicating message type.
Eg:

{
    "to"= 105;
    "id"= "64";
    "message"="HAS STARTED A VIDEO CONVERSATION";
    "self"= 0;
    "old"= "1";
    "sent"= 1411035449792;
    "from"= "73";
    "message_type"= "32";
}

Types of messages received in this callback:
HAS STARTED A VIDEO CONVERSATION
message_type = 32 : Incoming audio video group conference request

Note: Everytime a user joins the chatroom, last tens are received in the onChatroomMessageReceived: and onAVChatMessageReceived: callback blocks. This may include history audio-video conference messages also.

(v). (void(^)(NSError *error))failure
Invocation: Triggered when error occurs while subscribing to chatroom.
Response: NSError object representing error.
Usage:

[cometChatRoom subscribeToChatroomWithMode:YES onChatroomMessageReceived:^(NSDictionary *response) {
                                    }onActionMessageReceived:^(NSDictionary *response) {
                                    }onChatroomsListReceived:^(NSDictionary *response) {
                                    }onAVChatMessageReceived:^(NSDictionary *response) {
                                                    }failure:^(NSError *error) {
           }];

3. Create Chatroom
Create a chatroom in CometChat. Specify chatroomName, type of the chatroom (Refer to enum CHATROOM_TYPES for different types of chatroom). Specify chatroomPassword for password protected room, keep it as an empty string otherwise. (Ensure that chatroomName should not be nil or empty).

- (void)createChatRoom:(NSString *)chatroomName OfType:(NSInteger)type 
          withPassword:(NSString *)chatroomPassword
              success:(void(^)(NSDictionary *response))success
              failure:(void(^)(NSError *error))failure;

Return type: void
Callback:
(i). (void(^)(NSDictionary *response))success
Invocation: After a chatroom is successfully created.
Response: NSDictionary containing the chatroom ID of newly created chatroom.
Eg:

{
   chatroom_id: 5;
}

(ii). (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while creating chatroom.
Response: NSError object representing error.
Usage:

[cometchatChatroom createChatRoom:@"Friends" OfType:PUBLIC_CHATROOM 
                     withPassword:@"" 
                     success:^(NSDictionary *response) {
                   } failure:^(NSError *error) {
}];

Note: When a user creates a chatroom, he/she automatically joins that chatroom.

4. Join Chatroom
Join a specific chatroom by providing chatroomName, chatroomID and chatroomPassword. The password (if any) should be SHA-1 encoded. (Ensure that chatroomName and chatroomID should not be nil or empty).

- (void)joinChatroom:(NSString *)chatroomName 
       chatroomID:(NSString*)chatroomID 
       chatroomPassword:(NSString *)chatroomPassword
       success:(void(^)(NSDictionary *response))success
       failure:(void(^)(NSError *error))failure;

Return type: void
Callback:
(i). (void(^)(NSDictionary *response))success
Invocation: Triggered when the currently logged in user has entered a chatroom.
Response: NSDictionary containing ID of the joined room.
Eg:

{
    chatroom_id="5";
    push_channel="C_c49602157105d9d6383bb30695f771e8";
}

(ii). (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while joining chatroom.
Response: NSError object representing error.
Usage:

[cometchatChatroom joinChatroom:@"Friends" 
                     chatroomID:@"6" 
               chatroomPassword:@""
                   success:^(NSDictionary *response) {
                 } failure:^(NSError *error) {
}];

5. Invite user
Invite users to join the chatroom. Specify chatroomName, chatroomID. You have to be a part of the chatroom to invite any user. The other user will receive a chatroom invite message in his respective messageReceived callback. (Ensure that usersID array should not be empty).

- (void)inviteUsers:(NSArray *)usersID
         toChatroom:(NSString *)chatroomName
     withChatroomID:(NSString *)chatroomID
            success:(void(^)(NSDictionary *))success
            failure:(void(^)(NSError *error))failure;

Return type: void
Callback:
(i). (void(^)(NSDictionary *response))success
Invocation: Triggered when users are invited to the currently joined chatroom.
Response: NSDictionary containing ID of the successfully invited users.
Eg:

{
    users_invited=@[@"3",@"33",@"23"];
}

(ii). (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while inviting users to chatroom.
Response: NSError object representing error.
Usage:

[cometChatRoom inviteUsers:@[@"3",@"33",@"23"] toChatroom:@"Friends" withChatroomID:@"6" success:^(NSDictionary *response) { 
    } failure:^(NSError *error) {  
}];

Note: All the invited users will receive a join chatroom message of message_type = 11 in the onMessageReceived: callback block.

6. Send message
Send text/emoji message to currently joined chatroom. Specify chatroomName, chatroomID. (Ensure that logged-in user is member of a chatroom and also message must not be nil or an empty).

- (void)sendChatroomMessage:(NSString *)message
                 toChatroom:(NSString *)chatroomName
             withChatroomID:(NSString *)chatroomID
                withsuccess:(void (^)(NSDictionary *))success
                    failure:(void (^)(NSError *))failure;

Return type: void
Callback:
(i). (void(^)(NSDictionary *response))success
Invocation: After a message is delivered to the server.
Response: NSDictionary containing the original message and it’s id.
Eg:

{
  "id"=71;
  "m"="Hi! How are you?";
}

(ii). (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while sending message.
Response: NSError object representing error.
Usage:

[cometChatChatroom sendChatroomMessage:@"Hi! How are you?" toChatroom:@"Friends" withChatroomID:@"6" withsuccess:^(NSDictionary *response) {
            } failure:^(NSError *error) {
}];

7. Send Image

a) Send image message in the form of imageData to the currently joined chatroom. Specify chatroomName, chatroomID. (Ensure that logged-in user is member of a chatroom and imageData should not be nil).

- (void)sendImageWithData:(NSData *)imageData
               toChatroom:(NSString *)chatroomName
           withChatroomID:(NSString *)chatroomID
                  success:(void (^)(NSDictionary *))response
                  failure:(void (^)(NSError *))failure;

Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
Invocation: When an image is sent successfully to the chatroom.
Response: NSDictionary containing the message id of image message and original image data.
Eg:

{
    "id"="71";
    "m"="<ffd8ffe0 00104a46 49460001 01010048 00480000 ffdb0043 00030202 02020203 02020203 03030304 06040404 04040806 
            06050609 080a0a09 0809090a 0c0f0c0a 0b0e0b09 090d110d 0e0f1010 11100a0c 12131210 130f1010 10ffdb00 430103
            03 03040304 08040408 100b090b>";
}

(ii) (void(^)(NSError *error))failure
Invocation: If there’s an error while sending image.
Response: NSError containing error code.
Usage:

[cometChatChatroom sendImageWithData:imageData toChatroom:@"Friends" withChatroomID:@"6" 
                           success:^(NSDictionary *response) {  
                           } failure:^(NSError *error) {
}];

b) Send image at given imagePath to the currently joined chatroom. The image file should exist at the given path location. Specify chatroomName, chatroomID. (Ensure that logged-in user is member of a chatroom and imagePath should not be nil or empty).

- (void)sendImageWithPath:(NSString *)imagePath
                          toChatroom:(NSString *)chatroomName
                      withChatroomID:(NSString *)chatroomID
                             success:(void (^)(NSDictionary *))response
                             failure:(void (^)(NSError *))failure;

Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
Invocation: When an image at specified path is sent successfully.
Response: NSDictionary containing the message id of sent image message and the original image path.
Eg:

{
    "id"="71";
    "m"="/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/IMG1049.JPG";
}

(ii) (void(^)(NSError *error))failure
Invocation: If there’s an error while sending image.
Response: NSError containing error code.
Usage:

[cometchatChatroom sendImageWithPath:@“/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-
                                              89146DF4D1EB/Documents/IMG1049.JPG”  
                                     toChatroom:(NSString *)chatroomName
                                 withChatroomID:(NSString *)chatroomID
                                        success:^(NSDictionary *response) {  
                                      } failure:^(NSError *error) {
}];

8. Send Video

a) Send video at given videoPath to the currently joined chatroom. The video file should exist at the given path location and must be in mp4 format. Specify chatroomName, chatroomID. (Ensure that logged-in user is member of a chatroom and also nil or empty videoPath should not be given).

- (void)sendVideoWithPath:(NSString *)videoPath
               toChatroom:(NSString *)chatroomName
           withChatroomID:(NSString *)chatroomID
                  success:(void (^)(NSDictionary *))response
                  failure:(void (^)(NSError *))failure;

Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
Invocation: When a video at given path is sent successfully.
Response: NSDictionary containing the message id of video message and original video path.
Eg:

{                    
    "id"="74";
    "m"="/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/IMG0180.mp4";
}

(ii) (void(^)(NSError *error))failure
Invocation: If there’s an error while sending video.
Response: NSError containing error code.
Usage:

[cometchatChatroom sendVideoWithPath:@“/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-
                                   89146DF4D1EB/Documents/IMG0180.mp4” 
                  toChatroom:@"Friends" 
              withChatroomID:@"6"
                     success:^(NSDictionary *response) {  
                   } failure:^(NSError *error) {
}];

b) Send video using NSURL to the currently joined chatroom. NSURL can be obtained either by accessing
UIImagePickerControllerMediaURL property of a video when it is selected using UIImagePickerController or by converting path
of given video to NSURL. Specify chatroomName, chatroomID. (Ensure that logged-in user is member of a chatroom and also nil or empty videoURL should not be sent).

- (void)sendVideoWithURL:(NSURL *)videoURL
              toChatroom:(NSString *)chatroomName
          withChatroomID:(NSString *)chatroomID
                 success:(void (^)(NSDictionary *))response
                 failure:(void (^)(NSError *))failure;

Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
Invocation: When video message is sent successfully using given videoURL.
Response: NSDictionary containing the message id of sent video message and original videoURL.
Eg:

{
    "id"="71";               
    "m"="Users/MyPC/Desktop/72fd88e9abc28c04ef4b98d3bd62b483.3gp -- file:///”;
}

(ii) (void(^)(NSError *error))failure
Invocation: If there’s an error while sending video using given videoURL.
Response: NSError containing error code.
Usage:

[cometchatChatroom sendVideoWithURL:videoURL 
                 toChatroom:@"Friends" 
             withChatroomID:@"6"
                    success:^(NSDictionary *response) {  
                  } failure:^(NSError *error) {
}];

9. Send audio

Send audio at given audioPath to the currently joined chatroom. The audio file should exist at the given path location. Specify chatroomName, chatroomID. (Ensure that logged-in user is member of a chatroom and audioPath should not be nil or empty).

- (void)sendAudioWithPath:(NSString *)audioPath
               toChatroom:(NSString *)chatroomName
           withChatroomID:(NSString *)chatroomID
                  success:(void (^)(NSDictionary *))response
                  failure:(void (^)(NSError *))failure;

Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
Invocation: When an audio at specified path is sent successfully.
Response: NSDictionary containing the message id of sent audio message and the original audio path.
Eg:

{
    "id"="71";
    "m"="/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/sample.mp3";
}

(ii) (void(^)(NSError *error))failure
Invocation: If there’s an error while sending audio.
Response: NSError containing error code.
Usage:

[cometchatChatroom sendAudioWithPath:@“/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-
                                              89146DF4D1EB/Documents/sample.mp3”  
                          toChatroom:@"Friends" 
                      withChatroomID:@"6"
                             success:^(NSDictionary *response) {  
                           } failure:^(NSError *error) {
}];

10. Send file

Send file at given filePath to the currently joined chatroom. The file should exist at the given path location. Specify chatroomName, chatroomID. (Ensure that logged-in user is member of a chatroom and filePath should not be nil or empty).

- (void)sendFileWithPath:(NSString *)audioPath
              toChatroom:(NSString *)chatroomName
          withChatroomID:(NSString *)chatroomID
                 success:(void (^)(NSDictionary *))response
                 failure:(void (^)(NSError *))failure;

Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
Invocation: When a file at specified path is sent successfully.
Response: NSDictionary containing the message id of sent audio message and the original file path.
Eg:

{
    "id"="71";
    "m"="/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/samplefile.txt";
}

(ii) (void(^)(NSError *error))failure
Invocation: If there’s an error while sending file.
Response: NSError containing error code.
Usage:

[cometchatChatroom sendFileWithPath:@“/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-
                                              89146DF4D1EB/Documents/samplefile.txt” 
                          toChatroom:@"Friends" 
                      withChatroomID:@"6"
                             success:^(NSDictionary *response) {  
                           } failure:^(NSError *error) {
}];

11. Leave Chatroom
To exit and stop receiving messages from currently joined chatroom. Specify chatroomID.

- (void)leaveChatroom:(NSString *)chatroomID
              success:(void (^)(NSDictionary *))response
              failure:(void(^)(NSError *error))failure;

Return type: void
Callback Blocks:
(i) (void(^)(NSDictionary *response))response
Invocation: When the user is left from chatroom successfully.
Response: NSDictionary containing the chatroom ID of left chatroom.
Eg:

{
    "chatroom_id" = 6;
}

(ii) (void(^)(NSError *error))failure
Invocation: Triggered when error occurs while leaving chatroom.
Response: NSError object representing error.
Usage:

[cometChatChatroom leaveChatroom:@"6" success:^(NSDictionary *response) {
    }
    failure:^(NSError *error) {
                                
    }];

12. Get Chatroom list
Fetches the list of all chatrooms associated with logged-in user.

- (void)getAllChatrooms:(void(^)(NSDictionary *chatroomList))response
             failure:(void(^)(NSError *error))failure;

Return type: void
Callback:
(i). (void(^)(NSDictionary *chatroomList))response
Invocation: When chatroom list is requested.
Response: NSDictionary containing the chatroom list.
Eg:

{
  "_4"={
    "id"="4";
    "name"="room1";
    "online"="1";
    "type"="0";
    "i"="";
    "s"=0;
  },
  "_2"={
    "id"="2";
    "name"="room2";
    "online"="0";
    "type"="0";
    "i"="";
    "s"=0;
  }
}

(ii) (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while fetching list of chatrooms.
Response: NSError object representing error.
Usage:

[cometchatChatroom getAllChatrooms:^(NSDictionary *chatroomList) {
                   } failure:^(NSError *error) {
             }];

13. Delete chatroom

- (void)deleteChatRoom:(NSString *)chatroomID
                 success:(void(^)(NSDictionary *response))response
                 failure:(void(^)(NSError *error))error;

Return type: void
Callback:
(i). (void(^)(NSDictionary *response))response
Invocation: After chatroom is deleted.
Response: NSDictionary containing the chatroom ID of deleted chatroom.
Eg:

{
     chatroom_id = 5;
}

(ii). (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while deleting chatroom.
Response: NSError object representing error.
Usage:


[cometchatChatroom deleteChatRoom:@”5” 
             success:^(NSDictionary *response) {} 
             failure:^(NSError *error) {
}];

14. Rename chatroom

- (void)renameChatRoom:(NSString *)chatroomID
            toChatroom:(NSString *)chatroomName
               success:(void(^)(NSDictionary *response))response
               failure:(void(^)(NSError *error))error;

Return type: void
Callback:
(i). (void(^)(NSDictionary *response))response
Invocation: After chatroom is renamed successfully.
Response: NSDictionary containing the chatroom ID of renamed chatroom.
Eg:

{
    "chatroom_id" = 5;
}

(ii). (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while renaming chatroom.
Response: NSError object representing error.
Usage:

[cometChatChatroom renameChatRoom:@"5" toChatroom:@"Comet" success:^(NSDictionary *response) {
            } failure:^(NSError *error) {
            }];

15. Get Chatroom Memberslist

- (void)getChatroomMembers:(NSString *)chatroomId
                              success:(void (^)(NSDictionary *))success
                              failure:(void (^)(NSError *))failure;

Return type: void
Callback:
(i). (void(^)(NSDictionary *response))response
Invocation: Triggered when we get the members of the chatroom.
Response: NSDictionary containing the list of all users in the chatroom.
Eg:

{
    "_102" = {
        "id" = "102";
        "n" = "test-102";
        "a" = "img.png";
        "b" = "0";
    },
    "_73" = {
        "id" = "73";
        "n" = "test-73";
        "a" = "img.png";
        "b" = "0";
    }
}

(ii). (void(^)(NSError *error))failure;
Invocation: Triggered when error occurs while fetching chatroom members list.
Response: NSError object representing error.
Usage:

[cometChatChatroom getChatroomMembers:@"5" success:^(NSDictionary *response) {
    } failure:^(NSError *error) {
    }];

16. Checks if the user is a part of the chatroom corresponding to chatroomID

+ (BOOL)isSubscribedToChatroom:(NSString *)chatroomID;

Return type: BOOL
Usage:

[CometChatChatroom isSubscribedToChatroom:@"2"];

17. Unsubscribe from Chatrooms
To stop receiving chatroom messages, chatroom user’s list and chatroom updates. Also you will be exited from currently joined chatroom (if any).

- (void)unsubscribeFromChatRoom; 

Return type: void
Usage:

[cometchatChatroom unsubscribeFromChatRoom];

Please refer ‎error codes for failure responses.

Not finding what you need?

The CometChat team is here to help!