Chatrooms

To add Chatrooms support in your application,

Include the MessageSDKBinding.iOS in the .cs file of your ViewController.

include-file

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

1. Initialize the CometChatChatroom object as follows

 private  CometChatChatroom cometchatChatroom =  new CometChatChatroom();

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, Action<NSDictionary> message, Action<NSDictionary> actionMessage, Action<NSDictionary> chatroomList, Action<NSDictionary> chatroomMembersList, Action<NSDictionary> avchatMessage, Action<NSError> failure);

Return type: void
Callback Blocks:
(i). Action<NSDictionary>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

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

(ii). Action<NSDictionary>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) Action<NSDictionary>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). Action<NSDictionary>chatroomMembersList
Invocation: Triggered when we get the members of the current chatroom.
Response: NSDictionary containing the list of all the users in the current room.
Eg:

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

(v). Action<NSDictionary>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.

(vi). Action<NSError> failure
Invocation: Triggered when error occurs while subscribing to chatroom.
Response: NSError object representing error.
Usage:


private void OnChatroomMessageReceived(NSDictionary dict) {
  Console.WriteLine("OnChatroomMessageReceived : " + dict);
}

private void OnKicked(NSDictionary dict) {
  Console.WriteLine("OnKicked : " + dict);
}

private void OnBanned(NSDictionary dict) {
  Console.WriteLine("onAVChatMessageReceived : " + dict);
}

private void OnChatroomsListReceived(NSDictionary dict) {
  Console.WriteLine("OnChatroomsListReceived : " + dict);
}

private void OnChatroomMembersListReceived(NSDictionary dict) {
  Console.WriteLine("OnChatroomMembersListReceived : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

cometchatChatroom.SubscribeToChatroomWithMode(true,
                             (dict) => { this.onChatroomMessageReceived(dict); },
                             (dict) => { this.onKicked(dict); },
                             (dict) => { this.onBanned(dict); },
                             (dict) => { this.onChatroomsListReceived(dict); },
                             (dict) => { this.onChatroomMembersListReceived(dict); },
                             (err) => { this.OnFailure(err);});

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 (string chatroomName, nint type, string chatroomPassword, Action<NSDictionary> success, Action<NSError> failure);

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

{
   chatroom_id: 5;
}

(ii). Actionfailure;
Invocation: Triggered when error occurs while creating chatroom.
Response: NSError object representing error.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

CreateChatRoom.CreateChatRoom("Friends",
                              0,
                              "",
                             (dict) => { this.OnSuccess(dict); },
                             (err) => { this.OnFailure(err);});

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 (string chatroomName, string chatroomID, string chatroomPassword, Action<NSDictionary> success, Action<NSError> failure);

Return type: void
Callback:
(i). Action<NSDictionary> 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 current chatroom. 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 (string[] usersID, Action<NSDictionary> success, Action<NSError> failure);

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

{
    [email protected][@"3",@"33",@"23"];
}

(ii). Action<NSError> failure;
Invocation: Triggered when error occurs while inviting users to chatroom.
Response: NSError object representing error.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

cometchatChatroom.InviteUsers(["3","33","23"],
                                (dict) => { this.OnSuccess(dict); },
                              (err) => { this.OnFailure(err);});

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. (Ensure that logged-in user is member of a chatroom and also message must not be nil or an empty).

void SendChatroomMessage (string message, Action<NSDictionary> success, Action<NSError> failure);

Return type: void
Callback:
(i). Action<NSDictionary> 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). Action<NSError> failure;
Invocation: Triggered when error occurs while sending message.
Response: NSError object representing error.
Usage:



private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

cometchatChatroom.SendChatroomMessage("Hi! How are you?",
        (dict) => { this.OnSuccess(dict); },
                              (err) => { this.OnFailure(err);});
[cometchatChatroom sendChatroomMessage:@"Hi! How are you?"
              success:^(NSDictionary *response) {
            } failure:^(NSError *error) {
}];

7. Send Image

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

void SendImageWithData (NSData imageData, Action<NSDictionary>response, Action<NSError> failure);

Return type: void
Callback Blocks:
(i) Action<NSDictionary>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) Action<NSError> failure
Invocation: If there’s an error while sending image.
Response: NSError containing error code.
Usage:



private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

cometchatChatroom.SendImageWithData(imageData,
        (dict) => { this.OnSuccess(dict); },
                              (err) => { this.OnFailure(err);});

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

void SendImageWithPath (string imagePath, Action<NSDictionary>response, Action<NSError> failure);

Return type: void
Callback Blocks:
(i) Action<NSDictionary>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) Actionfailure
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”,
        (dict) => { this.OnSuccess(dict); },
                              (err) => { this.OnFailure(err);});
);

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. (Ensure that logged-in user is member of a chatroom and also nil or empty videoPath should not be given).

void SendAudioWithPath (string audioPath, Action<NSDictionary>response, Action<NSError> failure);

Return type: void
Callback Blocks:
(i) Actionresponse
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) Actionfailure
Invocation: If there’s an error while sending video.
Response: NSError containing error code.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

cometchatChatroom.SendAudioWithPath(“/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-
                                   89146DF4D1EB/Documents/IMG0180.mp4”,
        (dict) => { this.OnSuccess(dict); },
                              (err) => { this.OnFailure(err);});

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


void SendVideoWithPath (string videoPath, Action<NSDictionary>response, Action<NSError> failure);

Return type: void
Callback Blocks:
(i) Actionresponse
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) Actionfailure
Invocation: If there’s an error while sending video using given videoURL.
Response: NSError containing error code.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}
cometchatChatroom.SendVideoWithPath(videoURL,
        (dict) => { this.OnSuccess(dict); },
                              (err) => { this.OnFailure(err);});

9. Send audio

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

void SendAudioWithPath (string audioPath, Action<NSDictionary>response, Action<NSError> failure);

Return type: void
Callback Blocks:
(i) Action<NSDictionary>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) Action<NSError> failure
Invocation: If there’s an error while sending audio.
Response: NSError containing error code.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

cometchatChatroom.SendAudioWithPath(“/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-
                                              89146DF4D1EB/Documents/sample.mp3”,
            (dict) => { this.OnSuccess(dict); },
            (err) => { this.OnFailure(err);});

10. Send file

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

void SendFileWithPath (string filePath, Action<NSDictionary>response, Action<NSError> failure);

Return type: void
Callback Blocks:
(i) Action<NSDictionary>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) Action<NSError> failure
Invocation: If there’s an error while sending file.
Response: NSError containing error code.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}

cometchatChatroom SendFileWithPath(“/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-
                                              89146DF4D1EB/Documents/samplefile.txt”,
            (dict) => { this.OnSuccess(dict); },
            (err) => { this.OnFailure(err);});

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

void LeaveChatroom (Action<NSError> failure);

Return type: void
Callback: Actionfailure;
Invocation: Triggered when error occurs while leaving chatroom.
Response: NSError object representing error.
Usage:


private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}
cometchatChatroom.LeaveChatroom((err) => { this.OnFailure(err);});

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

void GetAllChatrooms (Action<NSDictionary>response, Action<NSError> failure);

Return type: void
Callback:
(i). Action<NSDictionary>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) Action<NSError> failure;
Invocation: Triggered when error occurs while fetching list of chatrooms.
Response: NSError object representing error.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}
cometchatChatroom.GetAllChatrooms((dict) => { this.OnSuccess(dict); },
          (err) => { this.OnFailure(err);});

13. Delete chatroom

void DeleteChatRoom (string chatroomID, Action<NSDictionary>response, Action<NSError>error);

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

{
     chatroom_id = 5;
}

(ii). Actionfailure;
Invocation: Triggered when error occurs while deleting chatroom.
Response: NSError object representing error.
Usage:


private void OnSuccess(NSDictionary dict) {
  Console.WriteLine("OnSuccess : " + dict);
}

private void OnFailure(NSError err) {
  Console.WriteLine("OnFailure : " + err);
}
cometchatChatroom.DeleteChatRoom(”5”,
                                (dict) => { this.OnSuccess(dict); },
        (err) => { this.OnFailure(err);});

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

bool IsSubscribedToChatroom (string chatroomID);

Return type: bool
Usage:

CometChatChatroom.IsSubscribedToChatroom("2");

15. 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!