Chatrooms

SDK allows you to have chat in chatrooms, share images, videos, invite other users in chatroom. To start with using the chatroom functionality in SDK you must have cometChatroom class instance, which can be obtained as below:

CometChatroom cometChatroom = CometChatroom.getInstance(context)

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:

subscribe(boolean true, new SubscribeChatroomCallbacks callbacks )

e.g

cometChatroom.subscribe(true, new SubscribeChatroomCallbacks() {
	@Override
	public void onMessageReceived(JSONObject receivedMessage) {}
	@Override
	public void onLeaveChatroom(JSONObject leaveResponse) {}
	@Override
	public void onError(JSONObject errorResponse) {}
	@Override
	public void gotChatroomMembers(JSONObject chatroomMembers) {}
	@Override
	public void gotChatroomList(JSONObject chatroomList) {}
        @Override
        public void onAVChatMessageReceived(JSONObject response);
        @Override
        public void onActionMessageReceived(JSONObject response);
});

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 the chatrooms available. This callback gets 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 :

joinChatroom(String roomId,String roomName,String roomPassword,Callbacks callback )

e.g

cometChatroom.joinChatroom("5", "Funzone", "", new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

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 :

sendMessage(String chatroomId, String message , Callbacks callback )

e.g

cometChatroom.sendMessage("1", "Hello Chatroom!", new Callbacks() {
    @Override
    public void successCallback(JSONObject response) {}
    @Override
    public void failCallback(JSONObject response) {}
});

The success response will be as follows :

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

4. Send image
You can send images to the chatroom which you have joined :

sendImage(Bitmap bitmap, Callbacks callback)	

e.g

cometChatroom.sendImage(bitmap, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

Here pass the bitmap of the image which you want to send to the chatroom. In successCallback you will get the following response:

{ "id": 23,  "original_file": "/storage/sdcard0/IMG20140929182949.jpg"}

The success response will provide you the id of the message sent and the file path of the image which is sent.

sendImage(File file, Callbacks callback);

You can send the image as file also, just provide file object which will be the file of image to be sent.
The success response will be same as above.

5. Send video
Send videos same like the image with following syntaxes

sendVideo(String filepath, Callbacks callback)

e.g

cometChatroom.sendVideo("/storage/sdcard0/video1.mp4",new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

The success response will be as follows :

{ "id": 23,  "original_file": "/storage/sdcard0/video1.mp4"}
sendVideo(File file, Callbacks callback)

You can also send the video by providing the File object which will represent the video to be sent.
The success response will be same as above.

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 :

sendAudioFile(String file,String userId,Callbacks callback)

e.g

cometChatroom.sendAudioFile(new File("/storage/sdcard0/audio1.aac"), "15", new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

The success response will be as follows:

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

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

sendFile(String file, Callbacks callback)

e.g

cometChatroom.sendFile(new File("/storage/sdcard0/document.txt"), new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

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:

leaveChatroom(Callbacks callback)

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 :

createChatroom(String roomName, String roomPassword, ChatroomType type, Callbacks callback)

e.g

cometChatroom.createChatroom("FCClub", "", ChatroomType.PUBLIC_CHATROOM, new Callbacks() {
        @Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

The ChatroomType in the Enum which provides you the type of chatroom you wish to create.
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 :

deleteChatroom(chatroomId, Callbacks callback)

e.g

cometChatroom.deleteChatroom("15", new Callbacks() {
        @Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

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 :

inviteUser(JSONArray userarray, Callbacks callback)

e.g:

JSONArray users = new JSONArray();
users.put(1);
users.put(2);
cometChatroom.inviteUser(users, new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {}
	@Override
	public void failCallback(JSONObject response) {}
});

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 those you get in the subscribe callbacks of the chatroom. The syntax is as follows:

getAllChatrooms(Callbacks callback)

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 :

 getCurrentChatroom() : String

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 :

 isSubscribedToChatroom() : boolean

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

 unsubscribe()

Not finding what you need?

The CometChat team is here to help!