Messaging
Learn how to send and receive messages, mark messages as delivered or read, etc.
Before you begin
- Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
- Configure QuickBlox SDK for your app. Check out our Setup page for more details.
- Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.
- Connect to the Chat server. See our Connection page to learn how to do it.
- Create a dialog. See our Dialogs page to learn how to do it.
Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.
Subscribe message events
To add the listener to receive messages in real-time, use the addDialogMessageListener()
method. The listener enables the app to listen to the incoming message.
You can add a message listener for a particular dialog if needed.
Send text message
To send a message to a private, group or public dialog, use the code snippet below.
You need to join the group and public dialog by calling the join()
method before you start chatting in a dialog. Once the dialog is joined, you can receive/send messages. See this section to learn how to join the dialog.
There are two ways to send any message to any dialog:
- Synchronous.
- Asynchronous.
You can use the most convenient for you, according to your application logic.
Make sure to set the saveToHistory
as true
to save the message on the server. If the saveToHistory
is set as false
, the message won’t be saved on the server. However, the message will be delivered to the user in either case.
Send message with attachment
Chat attachments are supported by the content API. In order to send a chat attachment, you need to upload the file to QuickBlox cloud storage and obtain a link to the file (file UID). Then you need to include this UID into the chat message and send it.
The same flow is supported on the message receiver’s side. When you receive a message with an attachment, you need to get the file UID, and then download the file from the cloud storage.
Send message with extra data
You have an option to extend the message with additional fields. Specify one or more key-value items in the message
. Using these items, you can implement the ability for a user to send self-location information to another user or notification messages signifying that a user has left a group, etc.
Argument | Required | Description |
---|---|---|
qbChatMessage | yes | Specifies message fields that should be set. |
Set the following fields of the qbChatMessage
:
Field | Required | Description |
---|---|---|
saveToHistory | no | Specifies if the message will be saved on the server. Set the saveToHistory as true to save the message on the server. |
body | no | A message text. |
property | no | Extra data. Specify any key-value pairs. In each pair, the key and value are both string values. |
Retrieve chat history
Every dialog stores its chat history that you can retrieve using the getDialogMessages()
method. The request below will return messages for a specific dialog limited to the 100 dialogs on the page.
If you want to mark all retrieved chat messages as a read, set the markAsRead
parameter as true
. If you decide not to mark chat messages as read, just set markAsRead
parameter as false
.
If you want to retrieve only messages updated after some specific date time and order the search results, you can apply operators. This is useful if you cache messages somehow and do not want to obtain the whole list of messages on every app start. Thus, you can apply search and sort operators to list messages on the page so that it is easier to view specific messages. The operators are set in the QBMessageGetBuilder
class.
If you want to get a paginated list of messages from the server, you can set the following pagination parameters in the QBMessageGetBuilder
class.
Pagination parameter | Description |
---|---|
skip | Skip N records in search results. Useful for pagination. Default (if not specified): 0. |
limit | Limit search results to N records. Useful for pagination. Default value: 100. |
Search operators
You can use search operators to search for the exact data that you need.
Search operators | Applicable to types | Applicable to fields | Description |
---|---|---|---|
lt(field, searchValue) | number, string, date | date_sent, sender_id, recipient_id, updated_at | Less Than operator. |
lte(field, searchValue) | number, string, date | date_sent, sender_id, recipient_id, updated_at | Less Than or Equal to operator. |
gt(field, searchValue) | number, string, date | date_sent, sender_id, recipient_id, updated_at | Greater Than operator. |
gte(field, searchValue) | number, string, date | date_sent, sender_id, recipient_id, updated_at | Greater Than or Equal to operator. |
ne(field, searchValue) | number, string, date | _id, message, date_sent, sender_id, recipient_id | Not Equal to operator. |
in(field, searchValue) | number, string, date | date_sent, sender_id, recipient_id | IN array operator. |
nin(field, searchValue) | number, string, date | date_sent, sender_id, recipient_id | Not IN array operator. |
or(field, searchValue) | number, string, date | date_sent, sender_id, recipient_id | All records that contain a value 1 or value 2. |
ctn(field, searchValue) | string | message | All records that contain a particular substring. |
Sort operators
You can use sort operators to order the search results.
Sort operator | Applicable to types | Description |
---|---|---|
sortAsc(field) | All types | Search results will be sorted in ascending order by the specified field. |
sortDesc(field) | All types | Search results will be sorted in descending order by the specified field. |
Update message
To update a message text , use the updateMessage()
method below.
You can also use the updateMessage()
method to mark message as delivered or read on the server. As a result, the ID of the user who has received the message or read it will be added to the array of delivered_ids
/read_ids
field, in the message model. However, the server won’t notify the sender with delivery or read receipt that informs about the change in the message model.
Delete message
Any user in the occupantIDs
can delete a message from the dialog. As a result, the message will be deleted from the current user history, without affecting the histories of other users.
The owner of the dialog can completely remove messages from all users’ histories. This is achieved by setting the forceDelete
parameter to true
.
Argument | Required | Description |
---|---|---|
messagesIDs | yes | A set of strings with messages IDs. |
forceDelete | yes | A boolean parameter. Delete message for everyone. Set it as true to perform. Only the owner can do it. |
Check if a message is sent
The message is considered as sent if it has been delivered to the server. To get to know that a message has been delivered to the server, make sure to enable a stream management before connecting to the Chat server. See this section to learn how to enable the stream management.
Thus, to track the event when your message becomes sent, use the QBChatDialogMessageSentListener
.
You should enable Stream Management before you do the chatService.login()
because the Stream Management is initialized while Chat login is performed.
The Stream Management defines an extension for active management of a stream between a client and server, including features for stanza acknowledgments.
Mark message as delivered
As a sender, you may want to be informed that a message has been successfully delivered to the recipient. The mark-as-delivered functionality allows to notify the sender about message delivery.
To track the event when the message has been delivered to the user, use the QBMessageStatusListener()
.
Use the deliverMessage()
method to mark a message as delivered. As a result, the server will notify a sender about the delivery receipt.
A message can be marked as delivered automatically by the server once a message is successfully delivered to the recipient. Set the markable
as true
using the sendMessage()
method if you want, as a sender, to receive message delivery receipts from other recipients. Thus, the markable
parameter enables the sender to request the delivery receipt. It also enables the recipient to confirm the message delivery. However, if markable
is false
or omitted, then you can notify a sender about the delivery receipt using the deliverMessage()
method.
Make sure to understand, that marking-as-delivered operation just confirms the fact of message delivery. The message acquires the delivered status when the message delivered event is received.
When a message is marked as delivered, the IDs of users who have received the message are stored in the message model, on the server. Thus, you can request a chat history from the server to get to know who received the message using the getDialogMessages()
method. See this section to learn how to retrieve chat history.
Mark message as read
As a sender, you may want to be informed that a message has been read by the recipient. The mark-as-read functionality allows to notify the sender that a message has been read.
To track the event when the message has been read by the user, use the QBMessageStatusListener()
.
Use the readMessage()
method to mark a message as read. As a result, the server will notify a sender about the read receipt.
When a message is marked as read, the IDs of users who have read the message are stored in the message model, on the server. Thus, you can request a chat history from the server to get to know who read the message using the getDialogMessages()
method. See this section to learn how to retrieve chat history.
Send typing indicators
You may want, as a sender, to let the recipient know that you are typing the message or have stopped typing the message. Use typing indicators as a form of chat-specific presence. Typing indicators allow to indicate if users are typing messages in a dialog at the moment.
There are the following typing notifications 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).
To track the event when the sender is typing the message, use the QBChatDialogTypingListener()
.
To track the event when the sender has stopped typing the message, use the QBChatDialogTypingListener()
.
To notify a recipient that a sender is typing the message, use the sendIsTyping()
method.
To notify a recipient that a sender had been composing a message but now has stopped, use the sendStopTypingNotification()
method.
Send system messages
There is a way to send system messages to other users about some events. For example, a system message can be sent when a user has joined or left a group dialog. These messages are handled over a separate channel and are not be mixed up with regular chat messages. Thus, they are handled by the QBSystemMessageListener
listener. See how to add the listener in the snippet below.
System messages are also not shown in the dialog history and, consequently, are not stored on the server. This means that these messages will be delivered only to online users. Send system messages using the sendSystemMessage()
method.
Argument | Description |
---|---|
qbChatMessage | Specifies system message fields that should be set. |
Set the following fields of the qbChatMessage
:
Field | Required | Description |
---|---|---|
recipientId | yes | ID of the recipient. |
dialogId | no | ID of the dialog. |
property | no | Extra data. Specify any key-value pairs. In each pair, the key and value are both string values. |
Was this page helpful?