Chat
ConnectyCube Chat API is built on top of Real-time(XMPP) protocol. In order to use it you need to setup real-time connection with ConnectyCube Chat server and use it to exchange data.
By default Real-time Chat works over secure TLS connection.
Connect to chat
Connect to chat using custom authentication providers
In some cases we don’t have a user’s password, for example when login via:
- Firebase phone authorization
- Custom identity authentication
- etc.
In such cases ConnectyCube API provides possibility to use ConnectyCube session token as a password for chat connection:
Connection status
The following snippet can be used to determine whether a user is connected to chat or not:
Disconnect
Reconnection
The SDK reconnects automatically when connection to Chat server is lost.
The following 2 callbacks are used to track the state of connection:
Dialogs
All chats between users are organized in dialogs. The are 4 types of dialogs:
- 1-1 chat - a dialog between 2 users.
- group chat - a dialog between specified list of users.
- public group chat - an open dialog. Any user from your app can chat there.
- broadcast - chat where a message is sent to all users within application at once. All the users from the application are able to join this group. Broadcast dialogs can be created only via Admin panel.
You need to create a new dialog and then use it to chat with other users. You also can obtain a list of your existing dialogs.
Create new dialog
Create 1-1 chat
You need to pass type: 3
(1-1 chat) and an id of an opponent you want to create a chat with:
Create group chat
You need to pass type: 2
and ids of opponents you want to create a chat with:
Create public group chat
It’s possible to create a public group chat, so any user from you application can join it. There is no a list with occupants, this chat is just open for everybody.
You need to pass type: 4
and ids of opponents you want to create a chat with:
Response example from ConnectyCube.chat.dialog.create(params)
- see
List dialogs
It’s common to request all your dialogs on every app login:
Response example from ConnectyCube.chat.dialog.list(filters)
- see
More filters available here
If you want to retrieve only dialogs updated after some specific date time, you can use updated_at[gt]
filter. This is useful if you cache dialogs somehow and do not want to obtain the whole list of your dialogs on every app start.
Update dialog
User can update group chat name, photo or add/remove occupants:
Add/Remove occupants
To add more occupants use push_all
operator.
To remove yourself from the dialog use pull_all
operator:
Response example from ConnectyCube.chat.dialog.update(dialogId, toUpdateParams)
- see
!> Important note: Only group chat owner can remove other users from group chat.
Remove dialog
The following snippet is used to delete a dialog:
This request will remove this dialog for current user, but other users still will be able to chat there. Only group chat owner can remove the group dialog for all users.
You can also delete multiple dialogs in a single request.
Clear dialog history
The following snippet is used to clear dialog history by ID:
This request will clear all messages in the dialog for current user, but not for other users.
Subscribe to dialog
In order to be able to chat in public dialog, you need to subscribe to it:
It’s also possible to subscribe to group chat dialog.
Response example from ConnectyCube.chat.dialog.subscribe(dialogId)
- see
Unsubscribe from dialog
Retrieve public dialog occupants
A public chat dialog can have many occupants. There is a separated API to retrieve a list of public dialog occupants:
Response example from ConnectyCube.chat.dialog.getPublicOccupants(dialogId)
:
Add / Remove admins
Options to add or remove admins from the dialog can be done by Super admin (dialog’s creator) only. Options are supported in group chat, public or broadcast.
Up to 5 admins can be added to chat.
Response example from ConnectyCube.chat.dialog.addAdmins(dialogId, adminsUsersIds)
/ConnectyCube.chat.dialog.removeAdmins(dialogId, adminsUsersIds)
- see
Update notifications settings
A user can turn on/off push notifications for offline messages in a dialog. By default push notification are turned ON, so offline user receives push notifications for new messages in a chat.
Response example from ConnectyCube.chat.dialog.updateNotificationsSettings(dialogId, enabled)
- see
Get notifications settings
Check a status of notifications setting - either it is ON or OFF for a particular chat.
Available responses: 1 - enabled, 0 - disabled.
Response example from ConnectyCube.chat.dialog.getNotificationsSettings(dialogId)
- see
Chat history
Every chat dialog stores its chat history which you can retrieve:
Response example from ConnectyCube.chat.message.list(params)
- see
If you want to retrieve chat messages that were sent after or before specific date time only, you can use date_sent[gt]
or date_sent[lt]
filter. This is useful if you implement pagination for loading messages in your app.
Send/Receive chat messages
1-1 chat
Group chat
The group chat join is not a required step anymore. You can send/receive chat messages in a group chat w/o joining it.
Before you start chatting in a group dialog, you need to join it by calling join
function:
Then you are able to send/receive messages:
When it’s done you can leave the group dialog by calling leave
function:
‘Sent’ status
There is a ‘sent’ status to ensure that message is delivered to the server.
In order to use the feature you need to enable it when you pass config in ConnectyCube.init
:
The following callback is used to track it:
‘Delivered’ status
The following callback is used to track the ‘delivered’ status:
The SDK sends the ‘delivered’ status automatically when the message is received by the recipient.
This is controlled by markable: 1
parameter when you send a message. If markable
is 0
or omitted, then you can send the delivered status manually:
‘Read’ status
Send the ‘read’ status:
‘Is typing’ status
The following ‘typing’ notifications are supported:
- typing: The user is composing a message. The user is actively interacting with a message input interface specific to this chat session (e.g., by typing in the input area of a chat window)
- stopped: The user had been composing but now has stopped. The user has been composing but has not interacted with the message input interface for a short period of time (e.g., 30 seconds)
Send the ‘is typing’ status:
Attachments (photo / video)
Chat attachments are supported with the cloud storage API. In order to send a chat attachment you need to upload the file to ConnectyCube cloud storage and obtain a link to the file (file UID). Then you need to include this UID into chat message and send it.
Response example from ConnectyCube.storage.createAndUpload(fileParams)
:
If you are running Node.js environment, the following code snippet can be used to access a file:
The same flow is supported on the receiver’s side. When you receive a message, you need to get the file UID and then download the file from the cloud storage.
In a case you want remove a shared attachment from server:
Attachments (location)
Sharing location attachments is nothing but sending 2 numbers: latitude and longitude.
These values can be accessed using any JS library available in npm registry.
On the receiver’s side the location attachment can be accessed the following way:
Edit message
Use the following code snippet to edit a message (correct message body). Other user(s) will receive the ‘edit’ message info via callback:
Also, you can update a message via HTTP API:
Delete messages
Use the following code snippet to delete a message. Other user(s) will receive the ‘delete’ message info via callback:
If you want to delete a message for yourself only, use the following API:
Response example from ConnectyCube.chat.message.delete(messageIds)
- see
This request will remove the messages from current user history only, without affecting the history of other users.
Unread messages count
You can request total unread messages count and unread count for particular dialog:
Response example from ConnectyCube.chat.message.unreadCount(params)
- see
Mark as read all chat messages
The following snippet is used to mark all messages as read on a backend for dialog ID:
Search
The following API is used to search for messages and chat dialogs:
Response example from ConnectyCube.chat.search(params)
- see
Please refer to Global search parameters for more info on how to form search params.
Chat alerts
When you send a chat message and the recipient/recipients is offline, then automatic push notification will be fired.
In order to receive push notifications you need to subscribe for it. Please refer to Push Notifications guide.
To configure push template which users receive - go to Dashboard Console, Chat Alerts page
Also, here is a way to avoid automatically sending push notifications to offline recipient/recipients. For it add the silent
parameter with value 1
to the extension
of the message.
After sending such a message, the server won’t create the push notification for offline recipient/recipients.
!> Note that currently push notifications are supported on mobile environment only.
Mark a client as Active/Inactive
When you send a chat message and the recipient/recipients is offline, then automatic push notification will be fired.
Sometimes a client app can be in a background mode, but still online. In this case it’s useful to let server know that a user wants to receive push noificattions while still is connected to chat.
For this particular case we have 2 handy methods: ‘markInactive’ and ‘markActive’:
The common use case for these APIs is to call ‘markInactive’ when an app goes to background mode and to call ‘markActive’ when an app goes to foreground mode.
Contact list
The Contact List API is rather straightforward. User A sends a request to become “friends” with user B. User B accepts the friend request. And now user A and B appear in each other’s roster.
Access contact list
You can access the contact list on your login to chat - the contact list object will be returned in callback.
Also, the following function gives you an access to contact list:
Response example from ConnectyCube.chat.contactlist.get()
:
Add user to contact list
To add a user to the contact list use the following method:
Note
Maximum number of contacts is 300.
Other user will receive a request to be added to the contact list - the onSubscribeListener
callback will be called:
Confirm the contact request
To confirm the request use the following method:
A user will be informed that you have accepted the contact request by onConfirmSubscribeListener
callback:
Reject the contact request
To reject the request use the following method:
A user will be informed that you have declined the contact request by onRejectSubscribeListener
callback:
Remove user from the contact list
To remove a previously added user from the contact list use the following method:
Contact list updates
You can also track contact list updates in real time by using delegates:
Privacy (black) list
Privacy list API allows enabling or disabling communication with other users in a chat. You can create, modify, or delete privacy lists, define a default list.
The user can have multiple privacy lists, but only one can be active.
Create privacy list
A privacy list must have at least one element in order to be created.
You can choose a type of blocked logic. There are 2 types:
- Block in one way. When you blocked a user, but you can send messages to him.
- Block in two ways. When you blocked a user and you also can’t send messages to him.
In order to be used the privacy list should be not only set, but also activated(set as default).
Activate privacy list
In order to activate rules from a privacy list you should set it as default:
Update privacy list
There is a rule you should follow to update a privacy list:
- If you want to update or set new privacy list instead of current one, you should decline current default list first.
Retrieve privacy list names
To get a list of all your privacy lists’ names use the following request:
Response example from ConnectyCube.chat.privacylist.getNames()
:
Retrieve privacy list with name
To get the privacy list by name you should use the following method:
Response example from ConnectyCube.chat.privacylist.getList(listName)
:
Remove privacy list
To delete a list you can call a method below or you can edit a list and set items to nil
.
Blocked user attempts to communicate with user
Blocked users will be receiving an error when trying to chat with a user in a 1-1 chat and will be receiving nothing in a group chat:
Get last activity
There is a way to get an info when a user was active last time, in seconds.
This is a modern approach for messengers apps, e.g. to display this info on a Contacts screen or on a User Profile screen.
Last activity subscription
Listen to user last activity status via subscription.
System messages
In a case you want to send a non text message data, e.g. some meta data about chat, some events or so - there is a system notifications API to do so:
Ping
Sometimes, it can be cases where TCP connection to Chat server can go down without the application (XMPP) layer knowing about it.
To check that chat connection is still alive or to keep it to be alive there is a ping method:
Also there is a way to enable automatic pings, via config: