Chatrooms

To add Chatrooms support in your application,

Include CustomUI.js in your index.html file to access the Custom UI functionality in your Cordova app

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

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



function callbackmeth(result){

   alert(result);

}

CustomUI.subscribeToChatroomWithMode(true,callbackmeth);

(i).
Invocation: After receiving any type of message in chatroom.
Response: Callback 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
 "chatroommessagereceived"={
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message" = "Hello";
    "sent" = 1415082627474;
    "message_type" = 10;
 }

Image message
 "chatroommessagereceived"={
    "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
 "chatroommessagereceived"={
    "from" = "test-9";
    "fromid" = 9;
    "id" = “12”;
    "message"="http://yoursite.com/cometchat/plugins/handwrite/uploads/9384627441b49aad5ef6bace29cdcd36.png";
    "sent"= 1411035449792;
    "message_type" = 13;
  }

Video message
 "chatroommessagereceived"={
    "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
 "chatroommessagereceived"={
    "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
 "chatroommessagereceived"={
    "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).
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 : Callback containing action type and chatroom id. Incase of DELETE_CHATROOM_MESSAGE_ACTION, message id is received instead of chatroom id.

Eg:

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

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

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

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

(iii)
Invocation: Triggered when we get the complete chatroom list from the server.
Response: Calllback containing the list of all chatroom and their details.
Eg:

"chatroomlistreceived"={
    "_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))chatroomMembersList
Invocation: Triggered when the we get the members of the current chatroom.
Response: Callback containing the list of all users in the current room.
Eg:

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

(v).
Invocation: When an audio video conference message is received.
Response: Callback containing audio video group conference messages and its details NSInteger indicating message type.
Eg:

"avchatmessagereceived"={
    "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.

2. 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).



function callbackmeth(result){

   alert(result);

}

CustomUI.createChatRoom("Friends",0,"",callbackmeth);

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

"success"={
   chatroom_id: 5;
}

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

3. 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).



function callbackmeth(result){

   alert(result);

}

CustomUI.joinChatroom("Friends","6","",callbackmeth);

(i).
Invocation: Triggered when the currently logged in user has entered a chatroom.
Response: Callback containing ID of the joined room.
Eg:

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

4. 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).



function callbackmeth(result){

   alert(result);

}

CustomUI.inviteUsers(["4","5","6"],callbackmeth);

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

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

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

5. 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).


function callbackmeth(result){

   alert(result);

}

CustomUI.sendChatroomMessage("Hi! How are you?",callbackmeth);

(i).
Invocation: After a message is delivered to the server.
Response: NSDictionary containing the original message and it’s id.
Eg:

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

6. 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).


function callbackmeth(result){

   alert(result);

}

CustomUI.sendImageWithDataChatroom(imageData,callbackmeth);

(i)
Invocation: When an image is sent successfully to the chatroom.
Response: Callback containing the message id of image message and original image data.
Eg:

"success"={
    "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>";
}

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).


function callbackmeth(result){

   alert(result);

}

CustomUI.sendImageWithPathChatroom("/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/IMG1049.JPG",callbackmeth);

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

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

7. 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).


function callbackmeth(result){

   alert(result);

}

CustomUI.sendVideoWithPathChatroom("/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/IMG0180.mp4",callbackmeth);

(i)
Invocation: When a video at given path is sent successfully.
Response: Callback containing the message id of video message and original video path.
Eg:

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

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


function callbackmeth(result){

   alert(result);

}

CustomUI.sendVideoWithURLChatroom(videoURL,callbackmeth);

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:

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

8. 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).


function callbackmeth(result){

   alert(result);

}

CustomUI.sendAudioWithPathChatroom("/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/sample.mp3",callbackmeth);

(i)
Invocation: When an audio at specified path is sent successfully.
Response: Callback containing the message id of sent audio message and the original audio path.
Eg:

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

9. 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).


function callbackmeth(result){

   alert(result);

}

CustomUI.sendFileWithPathChatroom("/var/mobile/Containers/Data/Application/F9778125-2506-4DED-9CDE-89146DF4D1EB/Documents/samplefile.txt",callbackmeth);

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

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

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

function callbackmeth(result){

   alert(result);

}

CustomUI.leaveChatroom(callbackmeth);

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



function callbackmeth(result){

   alert(result);

}

CustomUI.getAllChatrooms(callbackmeth);

(i).
Invocation: When chatroom list is requested.
Response: NSDictionary containing the chatroom list.
Eg:

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

12. Delete chatroom


function callbackmeth(result){

   alert(result);

}

CustomUI.deletechat("5",callbackmeth);

(i).
Invocation: After chatroom is deleted.
Response: NSDictionary containing the chatroom ID of deleted chatroom.
Eg:

"success"={
     chatroom_id = 5;
}

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


function callbackmeth(result){

   alert(result);

}

CustomUI.isSubscribedToChatroom("2",callbackmeth);

14. 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).



CustomUI.unsubscribeFromChatRoom();

Please refer ‎error codes for failure responses.

Not finding what you need?

The CometChat team is here to help!