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 chatrooms
As you subscribe to one-on-one chat to get the messages and other things, you also need subscribe to the chatroom to get the chatroom list, messages, etc. The syntax for subscribe is as follows:

Customui.subscribeToChatroomWithMode(flag,successCallback,failCallback);

When boolean flag is set to true you will receive emoji tags (e.g :smiley: format) instead of entire html content for emoji messages. If the subscription is successful you will start receiving the chatroom list, messages, etc in their respective callback.
The details about different callbacks is as follows:

1.1 onMessageReceived
This callback will get fired when someone sends messages in chatroom, the message response will be as follows

{
   "id":"456", "fromid":5, "message":"fsfsf","sent":1423645138, "from":"John Alexander", "text_color":"#000000", 
   "message_type":"10"
}

The fields in the message are as follows:
id : It represents the id of the message
fromid : This will represent the id of user who sent the message
message : This is message body
sent : This represents the timestamp of message when it is sent
from : This will provide you the name of user who sent the message
text_color : CometChat allows you to identify each user with different color in the chatroom, so if user has selected any color from the web, then that color will be provided here
message_type : This will provide the type of message you have received.
The message types are as follows :

Type Description
10 Normal text message , it also contains emoticons
11 Join chatroom message, this specifies message which is received when some user invites in chatroom
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

1.2 onLeaveChatroom
This callback will get fired when you leave the room. The response on this callback will be as follows:

{ chatroom_id: 7 }

1.3 onError
This callback will get fired when any subscription related error occurs. You will get the response in following format
{ code: 01, message: “Error msg” }
To get the various error codes, refer the ‎error codes

1.4 gotChatroomMembers
This callback fires when you join any chatroom or when any user joins/leave the chatroom. This will provide you the list of members present in the chatroom.
The chatroom members list will have structure as follows:

{
  "_102":{
    "id":"102", "n":"James smith", "a":"img.png", "b":"0"
  },
  "_73":{
    "id":"73", "n":"Tony stark", "a":"img.png", "b":"0"
  }
}

The description of the fields is as follows:
id : The id is userid of member
n : This is name of member
a : This is avatar of member
b : This field states that weather this user is banned from this chatroom or not

1.5 gotChatroomList
This callback provides you the list of chatroom available. This callback get fired when you subscribe, any new chatroom is created, or chatroom become active/inactive, or chatroom is deleted, any member join/leave the chatroom. The response of chatroom list is as follows:

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

The field description is as follows:
id : This is id of chatroom
name : This is name of chatroom
online : This provides the number of user present in the chatroom
type : This is type of chatroom.There are 3 type of chatrooms :
   public : 0 , password-protected :1, Invite-only : 2
i : This represents the password of chatroom ,if the chatroom is password protected then password will appear, else it will be empty
s : If the chatroom is created by moderator then this value will be non-zero

1.6 onAVChatMessageReceived
This callback fires when any group Audio Video conference related message occurs. You will receive the response in the following structure:

{
     "id":"478","message":"has started a video conversation.", "self":0,"sent":"1422594713", "from":"15", "message_type":"32"
}

The field description is as follows:
id : This is id of message you receive
message : This states the message related to group Audio Video chat
self : This will state that is this message sent by you or not
sent : This is timestamp of the message
from: This is id of user who has sent the message
message_type : This is type of group Audio Video chat message you received

1.7 onActionMessageReceived
This callback gets fired when you are kicked, banned from the chatroom, the chatroom is deleted by someone which you have joined or the message in the chatroom is deleted. The response is as follows:

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

OR

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

The various action type are as follows :

Action type Description
10 This action type states that you are kicked from the chatroom
11 This action type states that you are banned from the chatroom
12 This action type states that the chatroom which you have joined is deleted
13 This action type states that a message in the chatroom which you have joined is deleted.(Note the message in chatroom can only be deleted from the web)

2. Join chatroom
You have to join the chatroom in order to send/receive messages, images etc. The syntax for joining the chatroom is :

Customui.joinChatroom(chatroomName,chatroomID,chatroomPassword,successCallback,failCallback);

You have to determine the type of chatroom before joining it, if the room is password protected then you need to pass the password. The password must be encoded in SHA-1 format. If the room is not password protected then just leave the password field as empty string.
The success response for join chatroom is as follows :

{"chatroom_id": "5","push_channel":"C_c49602157105d9d6383bb30695f771e8"}

The push_channel is the channel on which you need to subscribe to receive push notifications for chatroom messages.
Note that the push_channel is different for each chatroom.

3. Send Message
Once you have joined the chatroom , then you can send the messages in it. The syntax is as follows :

Customui.sendChatroomMessage(message,chatroomID,successCallback,failCallback);

The success response will be as follows :

{  "id": 85,  "m": "Hello Chatroom!" }

4. Send images
You can send images to other user by two ways as follows :

Customui.sendImageWithPathChatroom(imagePath,successCallback,failCallback);

1. imagePath : This is image path that you want to share, for example, /storage/sdcard0/images.png

The success response will be as follows:

{ "id": 23,  "original_file": "/storage/sdcard0/images.png"}

5. Send videos
Send videos in the same way like the image with the following syntax:

Customui.sendVideoWithPathChatroom(videoPath,successCallback,failCallback);

1. videoPath : This is video path that you want to share, for example, /storage/sdcard0/video1.mp4

The success response will be as follows:

{ "id": 52,  "original_file": "/storage/sdcard0/video1.mp4"}

6. Send audio file
Now you can send the audio files from your mobile with sendAudioFile() function. You can also use this function to send the voice notes. Just record them, save them and send this saved file.
The syntax for this function is as follows :

Customui.sendAudioWithPathChatroom(audioPath,successCallback,failCallback)

1. audioPath : This is audio path that you want to share, for example, /storage/sdcard0/audio1.aac

The success response will be as follows:

{ "id": 52,  "original_file": "/storage/sdcard0/audio1.aac"}

7. Send file
Now you can send most popular file formats from your mobile with sendFile() function e.g txt, pdf, doc etc.
The syntax for this function is as follows :

Customui.sendFileChatroom(filePath,successCallback,failCallback);

The success response will be as follows:

{ "id": 52,  "original_file": "/storage/sdcard0/document.txt"}

8. Leave chatroom
Leave the chatroom any time you wish. You will not receive any messages from the chatroom you left. The syntax will be as follows:

Customui.leaveChatroom(successCallback,failCallback);

The chatroom which you have joined will be left.

9. Create chatroom
You can create chatroom any time you wish. The syntax is as follows :

Customui.createChatRoom(chatroomName,chatroomType,chatroomPassword,successCallback,failCallback);

The chatroomType in the Enum which provides you the type of chatroom you wish to create. For example,PUBLIC_CHATROOM or INVITE_ONLY or PASSWORD_PROTECTED.

The success response of this call is as follows:

{ chatroom_id: 7  }

10. Delete chatroom
You can delete the chatroom you have created. The syntax is as follows :

 Customui.deleteChatRoom(chatroomID,successCallback,failCallback);

The success response of this call is as follows :

{ chatroom_id:15 }

11. Invite user:
You can invite other users in the chatroom. The syntax for inviting user is as follows :

 Customui.inviteUsers(usersiDArray,successCallback,failCallback);

e.g:

You can invite users only in the chatroom which you have joined
Note that success callback will not get fired.

12. Get all chatrooms
You can get a list of all chatrooms any time, other than you get in the subscribe callbacks of the chatroom. The syntax is as follows:

 Customui.getAllChatrooms(successCallback,failCallback)

You will get the list of the chatroom as follows:

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

13. Get current chatroom
Know which chatroom you have joined currently. The syntax is as follows :

 Customui.getCurrentChatroom(successCallback,failCallback);

This function will return you string which represents the chatroom id.

14. Check subscribe status
This function will check whether you have subscribed to chatroom or not and return boolean response. The syntax is as follows :

Customui.isSubscribedToChatroom(chatroomID,successCallback,failCallback);

15. unsubscribe
To stop receiving the updates,messages from the chatroom you need to unsubscribe to the chatroom. The syntax is as follows :

 Customui.unsubscribeFromChatRoom(successCallback,failCallback);

Not finding what you need?

The CometChat team is here to help!