One-on-One

To use One-on-One functionality, you need to have instance of CometChat :

1. Get CometChat instance
To get the CometChat instance you can write simple script as follows:

CometChat cometchat = CometChat.GetInstance(this, "API-KEY");

The API-KEY is utilized for using user management API which is avaliable with CometChat v5.7 for custom coded integration. You can get API-KEY from CometChat administration panel. If you are not using user management API then you can pass “” value to parameter.

2. Subscribe to CometChat
Subscribe to CometChat using subscribe() function to get the messages, userinfo etc.
The Syntax is as follows:

Subscribe(Boolean flag, SubscribeCallbacks subscribecallback)

e.g

cometchat.Subscribe(true,new MysubcriptionCallback(successObj=>this.onSucess(successObj)));

Where MysubcriptionCallback is as follows :

class MysubcriptionCallback : Java.Lang.Object, ISubscribeCallbacks
	{
		Action _onMessageReceived;
		Action _gotOnlineList;
		...
		...
		...

		public MysubcriptionCallback(Action onMessageReceived, Action gotOnlineList = null,....)
		{
			this._onMessageReceived = onMessageReceived;
			this._gotOnlineList = gotOnlineList;
			...
			...
			...
		}

		public void OnMessageReceived (Org.Json.JSONObject p0)
		{
			Console.WriteLine ("inside the mycallback  onMessageReceive {0}", p0);
			this._onMessageReceived.Invoke(p0);
		}

		public void GotOnlineList (Org.Json.JSONObject p0)
		{
			Console.WriteLine ("inside the mycallback Got online list {0}", p0);
			this._gotOnlineList.Invoke(p0);
		}

		...
		...
		...
	}

When the boolean flag is set to true you will receive emoji tags (e.g :smiley: format) instead of the entire html content for emoji messages. Once subscription in successful, you will start receiving your profile information, updated buddy list, one-on-one chat messages and announcements in their respective callback methods.
The details about all the callbacks are as follows:

2.1. onMessageReceived()
This callback is invoked when any new message is sent to you. The message response is as follows:

{
message:{
    "id": "64", "message": "654", "self": 0, "old": "1","sent": 1411035449792, "from": "73", "message_type":"10"
 }
}

The description of fields in message is as follows:
from : Id of user from which message is received
id : This is the id of message
message : This will be the message body
message_type : This will depict the type of message
old : This will specify that the message is old
self : This will specify that the message is sent by you or not, if self is “1” then the message is sent by you
sent : This will be the timestamp of the message

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
17 Regular file like .txt, .pdf, the file’s download link will be available in message
18 Sticker message

2.2. gotProfileInfo()
This callback will be invoked when you subscribe and any updates made to your profile information.
The response is as follows:

{
    "id":"5","n":"MyName", "l":"link","a": "avatar.png","s":"available", "m":"I'm available","push_channel":"C_e9d4eb1e42bc17d6540e46a72af90c81","push_an_channel":"ANN_1c361dfb0ac82a9f988ed4c1f4bcb785"
}

The descriptions of fields is as follows:
id : This represents the id of user who is logged in
n : Name of user who is logged in
l : If the user has set any link for his profile then it will appear here
a : This will be the url to your profile picture
s : This is your status
m : This is status message
push_channel : If you are implementing push notification in your app then you need to use this push notification channel on which you will subscribe so that you will get push notifications from CometChat
push_an_channel : You have to use this push notification channel to get the push notifications for announcement sent from CometChat administration panel

2.3. gotOnlineList()
This callback is invoked when the userlist is updated.
The response is as follows:

{
  {
    "id": "1","n": "User1", "a": "avatar.png", "d": "1", "s": "available","m": "I'm available"
  },
  {
    "id": "2", "n": "User2", "a": "avatar.png", "d": "1",  "s": "available","m": "I'm available"
  }
}

Note: You may get some extra parameters in the json response as per your version of CometChat, you can use them as per your requirements.
The fields in the response are pretty same as you get for your profile. The new field is d which specifies whether a user is logged in from the device or web.
With CometChat v5.7 you will get 2 new parameters “ls” and “lstn”. These 2 paramters provide information about last seen.
“ls” parameter provides you the timestamp which is a 10 digit value. You can use this value to show the last seen of a user in your app. You can also use this value to show that a user is online if the time difference between your mobile and the obtained time is less than 1 minute or anything suitable to your need.
“lstn” is a flag which tells whether the last seen is enabled or disabled for a user. The value “1” suggests that user has disabled last seen, so you cannot see his last seen.
With CometChat v5.7 and CometService enabled you will find one more parameter as “ch”. This is a channel for the user which you use in isTyping() function.

2.4. onError()
If there are any error while subscription or using SDK, then this callback will fire. The response will be as follows:

{  code: 01, message: "Error in connection" }

To get the various error codes, refer the ‎error codes

2.5. gotAnnouncement()
When any announcement is sent from the admin, this callback will get invoked. The response is as follows:

{ "id":"1", "m":"Announcements test","t":"1416576743" }

The id is the announcement id, m is the message of announcement and t is time of when the announcement is sent.

2.6. onAVChatMessageReceived()
This callback will get fired when any Audio/Video Chat related messages are received. The response is as follows:

{
 "id":106,"from":1,"message":"AVCHAT_INCOMING_CALL","sent":1448355920,"self":0,"callid":"fb19b85692a86ea149838df058fd3d17","message_type":32,"pluginType":1
} 
OR
{
 "id":10678,"from":1,"message":"AUDIOCHAT_INCOMING_CALL","sent":1448355885,"self":0,"callid":"a1836634d06ab7b06374f03ec6623eb9","message_type":32,"pluginType":0
}OR
{
"id":25085,"from":15,"message":"INCOMING_AVBROADCAST_REQUEST","sent":1453361688,"self":0,"callid":"d609672ced84b3fec6d84dad0d9cbda7","message_type":41,"pluginType":2
}

The pluginType will tell you whether the message is for audio call or audio/video call. It has the following values
– "0": Value "0" indicates that the message is for Audio call
– "1": Value "1" indicates that the message is for Audio/Video call
– "2": Value "2" indicates that the message is for Audio/Video broadcast

The "callid" is the identity number for the call, use this callid when you end/accept/start the call.
The message types for Audio/Video Chat are as follows:

Type Description
31 This will state that the other user has accepted the Audio/Video Chat request sent by you
32 This states that someone has sent you an Audio/Video Chat request
33 This states that you have a call going on and some other user tries to call you
34 This states that Audio/Video Chat call is ended
35 This states that your Audio/Video Chat request is rejected by the other person
36 This states that you can cancel the Audio/Video Chat request which is sent by you
37 This states that the other user is not answering to your request for a particular time
38 This states that other person has sent busy response to your Audio/Video Chat request

Audio/Video broadcast the message types are as follows:

Type Description
41 This will state that the message is for incoming broadcat request
42 This states that broadcaster has ended the broadcast
43 This states that someone has invited you for the broadcast

2.7 onActionMessageReceived()
This callback gets fired when any special message is received like isTyping… This callback provides a JSONObject as follows:

{"action":"typing_start","from":"12","sent":"1450849776899"}
{"action":"typing_stop","from":"12","sent":"1450849780098"}

The description of the parameters in response are as follows:
action: This specifies for which action this callback is fired like typing_start means other user has started typing.
from: This specifies from whom this action is generated like typing_start is from user 12, so user 12 is typing message to you.
sent: This specifies the timestamp of action.

This callback will also provide you information regarding message read, delivered information. You will get information as follows:

{"action":"message_deliverd","from":"12","message_id":"1201"}
{"action":"message_read","from":"12","message_id":"1201"}

Field action and from serves the same purpose as above, the parameter message_id will specify you that which message is delivered or read. If you get read message and message_id as 0, then that means user has read all the messages in his chat window so you have to mark all messages as read.

3. Send messages
Send a chat message to your friend with the following function:

SendMessage(String RECEIVER_USER_ID, String messages, Callbacks callback)

e.g

cometchat.SendMessage("15","Hi! How are you?",new MyCallback(successObj=>this.onSucess(successObj)));

Where MyCallback is as follows :

class MyCallback : Java.Lang.Object, ICallback{
     Action <Org.Json.JSONObject> _onSuccess;
     Action <Org.Json.JSONObject> _onFail;
     public MyCallback(Action<Org.Json.JSONObject> onSuccess, Action<Org.Json.JSONObject> onFail = null)
      {
	this._onFail = onFail;
	this._onSuccess = onSuccess;
      }
      void SuccessCallbac(JSONObject){...}
      void FailCallback(JSONObject){...}
}

In successcallback you will get the following response:

{  "id": 71,  "m": "Hi! How are you?" }

Here the id is the message id , and m will the message which you sent. You can also get "from" field in the json response which will contain the id of the user to whom you sent the message.

4. Send images
You can send an image as a file, just provide a file object which will be the file of the image to be sent.

SendImage(File file, String RECEIVER_USER_ID, Callbacks callback)

e.g

cometchat.SendImage(file,"15",new MyCallback(successObj=>this.onSucess(successObj)));

In successcallback you will get the following response:

{ "id": 23,  "original_file": "[email protected]"}

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

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

5.1

SendVideo(String filepath, String RECEIVER_USER_ID, Callbacks callback)

e.g

cometchat.SendVideo("/storage/sdcard0/video1.mp4","15",new MyCallback(successObj=>this.onSucess(successObj)));

The success response will be as follows:

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

5.2

SendVideo(File file, String RECEIVER_USER_ID, 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

cometchat.SendAudioFile(file,"15",new MyCallback(successObj=>this.onSucess(successObj)));

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 :

SendFile(File file,String userId,Callbacks callback)

e.g

cometchat.SendFile(file,"15",new MyCallback(successObj=>this.onSucess(successObj)));

The success response will be as follows:

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

8. Get all announcements
Get all announcements, which are sent by the admin. The syntax is as follows:

GetAllAnnouncements(Callbacks callback)

e.g

cometchat.GetAllAnnouncements(new MyCallback(successObj=>this.onSucess(successObj)));

The success response will be as follows:

{
  "_2":
    { "id":"2", "m":"Test 2", "t":"1416576762"},
  "_1":
    { "id":"1", "m":"Announcements test","t":"1416576743" }
}

The response is nested JSON object, the id is the announcement id, m is the announcement sent by the admin, t is the timestamp of announcement when the announcement is sent.

9. Get online users
You can get the user list any time when you require.
The syntax is

GetOnlineUsers(Callbacks callback)

The success response will be same as you get the user list in gotOnlineUsers() of subscribecallbacks.

10. Set your status
CometChat allows you to set the status for your profile. You can set any one of the “Available”,”Busy”,”Invisible”,”Offline” status.
The syntax is as follows:

SetStatus(StatusOption enum,Callbacks callback)

e.g

cometchat.SetStatus(Com.Inscripts.Enums.StatusOption.Available,new MyCallback(successObj=>this.onSucess(successObj)));

Here the StatusOption is the enum, you can select the desired enum value.
You will get the following success message:

{  message:"status updated" }

11. Set status message
As you can set your status, you can also set the status message for your profile.
The syntax is as follows:

SetStatusMessage(String STATUS_MESSAGE,Callbacks callback)

e.g

cometchat.SetStatusMessage("What a beautiful day!!",new MyCallback(successObj=>this.onSucess(successObj)));

The success response will be

{ message:"status message updated" }

12. Get chat history
Get your previous message from the CometChat when required.
The syntax is as follows:

GetChatHistory(Long USERID,Long MESSAGEID,Callbacks callback)

e.g

cometchat.GetChatHistory(10,102,new MyCallback(successObj=>this.onSucess(successObj)));

The first parameter is the user’s ID to whose previous message you want to fetch. The second parameter is the message id from which the older message will be fetched i.e message with id older than “102” will be fetched. The history will be received in the JSONObject under the key “history”. Pass “-1” as messageid to get all the messages.
The success response will be as follows:

{
  history = [
   { 
     from = 14; id = 4843;  message = "HI"; "message_type" = 10; old = 1;  self = 1;  sent = 1429771974000;
   },
  .
  .
 ]
}

Here the history is JSONArray consisting of all the messages, the message contains following fields:
from : Id of user from which message is received
id : This is the id of message
message : This will be the message body
message_type : This will depict the type of message
old : This will specify that the message is old
self : This will specify that the message is sent by you or not, if self is “1” then the message is sent by you
sent : This will be the timestamp of the message

13. Block the user
You can block a user so that messages from that user cannot be sent or received. Also, the user which is blocked will not be listed in your user’s list.
The Syntax is as follows:

BlockUser(String toId,Callbacks callback)

e.g

cometchat.BlockUser("15",new MyCallback(successObj=>this.onSucess(successObj)));

If the user is blocked successfully, you will get the following response:

{  "id":"15" }

14. Get blocked users list
Once you block any user, the user is removed from the user’s list. To unblock the user, call the getBlockedUserList() function to see which users are blocked.
The syntax is as follows:

GetBlockedUserList(Callbacks callback)

e.g

cometchat.GetBlockedUserList(new MyCallback(successObj=>this.onSucess(successObj)));

In success response you will get data as:

{
  "73": { "id":"73", "name":"user73" },
  "700": { "id":"70","name":"user70"}
}

The result is nested JSONObject containing id and name of the blocked user.

15. Unblock the user
To unblock the user which is blocked by you, you can use unblockUser() function. The syntax is as follows:

UnblockUser(String toId,Callbacks callback)

e.g

cometchat.UnblockUser("15",new MyCallback(successObj=>this.onSucess(successObj)));

If the user is unblocked successfully, you will get the following response:

{  "id":"15" }

16. Check if CometChat installed
Check whether CometChat is installed on your website. The syntax is as follows:

IsCometChatInstalled(String url,Callbacks callback)

e.g

cometchat.IsCometChatInstalled("http://www.example.com/cometchat/",new MyCallback(successObj=>this.onSucess(successObj)));

This will check whether CometChat is installed on the “example.com” or not, and if it is installed then it will return the complete URL till the CometChat directory.
The success response is as follows:

{ cometchat_url:http://www.example.com/cometchat/ }

17. Translate the messages
Translate all your incoming messages to the language you desire. Note that this feature only works with Google translate API, so make sure that you have selected Google API and provided valid Google-key in the admin panel of CometChat.
The syntax is as follows:

SetTranslateLanguage(Languages ENUM,Callbacks callback)

e.g

cometchat.SetTranslateLanguage(Com.Inscripts.Enums.Languages.Italian,new MyCallback(successObj=>this.onSucess(successObj)));

Here Languages is enum consisting of languages available in which messages can be translated. You will get {"Selected language":"Spanish"} response for success. To stop the translation use ‘Default’ enum value.

18. Broadcast a message
Now you can broadcast a message to multiple users, for this, you can use a broadcastMessage() function.
The syntax is as follows:

BroadcastMessage(String message, JSONArray users, final Callbacks callbacks)

e.g

Org.Json.JSONArray jsonArray = new Org.Json.JSONArray();
	jsonArray.Put(1);
	jsonArray.Put(2);
	jsonArray.Put(3);

cometchat.BroadcastMessage("HI",jsonArray,new MyCallback(successObj=>this.onSucess(successObj)));

19. Typing …
Now you can make your chat more alive by sending typing.. information to other user, so the other user will know you are typing some text to him.
Note that this feature will only work from CometChat v5.7 onwards and by using CometService.
To send a message that you are typing you need to use isTyping() function. The syntax of this function is as follows:

IsTyping(boolean istyping, String channel, Callbacks callbacks)

e.g

cometchat.IsTyping(true,"49086e3ef2d52d104fa0bc7cade215cf",new MyCallback(successObj=>this.onSucess(successObj)));

The parameters description is as follows:
isTyping: This is boolean value , pass true when you start typing, pass false when you stop typing
channel: This is the channel id to whom you are chatting with. You will get the channel from the user list as “ch” channel when you enable the CometService.
callback: Will result in fail callback if CometService is not enabled for your site. To get the various error codes, refer the ‎error codes. There will not be any successCallback fired.

20. Get user profile information
You can fetch details of any user on the fly with the help of function getUserInfo(). The syntax for this function is as follow:

getUserInfo(String userid,Callbacks callback)

e.g.

cometchat.getUserInfo("12",new Callbacks() {
	@Override
	public void successCallback(JSONObject response) {
	}

	@Override
	public void failCallback(JSONObject response) {
	}
});

The success response will be as follow:

{
    "id": "12", "n": "John", "l": "", "d": "0", "a": "",
    "s": "offline", "m": "Wonderful day", "ch": "cc735e0970a1e82bb6738e59b881cb23","ls": "1451218483", "lstn": "0"
}

21. Get plugin information
Check which plugins are enabled in SDK by calling getPluginInfo() function. The syntax for this function is as follows

GetPluginInfo(Callbacks callback)

e.g

cometchat.GetPluginInfo(new MyCallback(successObj=>this.onSucess(successObj)));

You will get success response as follows

{"avchat_enabled":"1","audiochat_enabled":"1","createchatroom_enabled":"1","blockuser_enabled":"0","mediasharing_enabled":"1","chatroom_mediasharing_enabled":"1","realtime_translate_enabled":"0"}

22. Send message delivered, read information
Make your chat more interactive by sending message read, delivered information. Note that this feature only works with CometService and CometChat 5.7 onwards.
Message delivered information is sent by SDK itself, however, if you wish you can send this message by yourself also. To do so use sendDeliverdReceipt() function. The syntax for this function is as follows:

SendDeliverdReceipt(String messageid, String channelId, Callbacks callbacks)

e.g

cometchat.SendDeliverdReceipt("5","49086e3ef2d52d104fa0bc7cade215cf",new MyCallback(successObj=>this.onSucess(successObj)));

Pass the message id for which you wish to send the information that this message is delivered to you. Pass the channel to whom you wish to send the delivery message. This channel you will get in the userlist information.

To send the information that you have read a particular message use sendReadReceipt() function. The syntax for this function is as follows:

sendReadReceipt(String messageid, String channelId, Callbacks callbacks)

e.g

cometchat.SendDeliverdReceipt("5","49086e3ef2d52d104fa0bc7cade215cf",new MyCallback(successObj=>this.onSucess(successObj)));

Pass “0” for the message id to state that you have read all the messages in your chat window.

23. Unsubscribe from CometChat
To stop receiving all the updates from the CometChat i.e stop receiving messages, userlist etc., you can call unsubscribe () function. The syntax is as follows:

Unsubscribe()

Not finding what you need?

The CometChat team is here to help!