Dialogs
Learn how to create and manage dialogs.
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.
Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.
Dialog types
All chats between users are organized in dialogs. There are 3 types of dialogs:
- private dialog - a dialog between 2 users.
- group dialog - a dialog between the specified list of users.
- public dialog - an open dialog. Any user from your app can be joined to it.
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 dialog
To create a private dialog, you need to set the dialog type field to private
and ID of the opponent you want to create a chat with.
To create group dialog for a predefined number of occupants, you need to set the dialog type field to group
and IDs of opponents you want to create a chat with.
It’s possible to create a public dialog, so any user from your application can be joined to it. There is no list of occupants. This dialog is open for everybody. You need to set the dialog type field to public
.
Create dialog with custom parameters
Any dialog can be extended with additional parameters whether it is a private, group, or public. These parameters can be used to store additional data. Also, these parameters can be used in dialogs retrieval request.
To start using additional parameters, create an additional schema of your parameters. This is a Custom Objects class. Just create an empty class with all fields that you need. These fields will be additional parameters in your dialog. See this section to learn how to create a schema using Custom Objects.
Then, specify the parameters defined in the schema in a new dialog.
Join dialog
Before you start chatting in a group or public dialog, you need to join it by calling the join()
method. If you’ve successfully joined the dialog, you can send/receive messages in real-time. See this section to learn how to send/receive messages.
Let’s see, how the join()
method is used with regard to the dialog type.
Capabilities | Public | Group | Private |
---|---|---|---|
Join | ✓ | ✓ | ✗ |
You can join a group dialog only if your user ID is present in the occupantIDs
array, in the dialog model.
Your user ID is added to the occupantIDs
array if you create a dialog or you are added to the dialog by the other user. See this section to learn how to add occupants to the group dialog.
To subscribe to the onJoinOccupant
event, use the code snippet below. As a result, you will be notified by the SDK about the onJoinOccupant()
event whenever the occupant has joined the current group dialog.
Leave dialog
You can leave the group and public dialog by calling the leave()
method. If the dialog is left, you can’t send/receive messages. To be able to receive/send messages, you need to join it.
SwiftObjective-
Let’s see, how the leave()
method is used with regard to the dialog type.
Capabilities | Public | Group | Private |
---|---|---|---|
Leave | ✓ | ✓ | ✗ |
When a group dialog is left, your user ID is still present in the occupantIDs
array, in the dialog model. As a result, the dialog will still be present in the list of dialogs and you will still have access to the chat history.
To remove yourself from the group dialog, use the update()
method. See this section to learn how to remove occupants from the group dialog.
To subscribe to the onLeaveOccupant
event, use the code snippet below. As a result, you will be notified by the SDK about the onLeaveOccupant()
event whenever the occupant has left the current group dialog.
Retrieve online users
You can get a list of online users from the chatDialog
dialog. Call the requestOnlineUsers()
method to get the list of online users who are joined to the dialog. As a result, a completion block with an array of user IDs or failure error is called.
Let’s see, how the requestOnlineUsers()
method is used with regard to the dialog type.
Capabilities | Public | Group | Private |
---|---|---|---|
Retrieve online users | ✗ | ✓ | ✗ |
You can retrieve online users from the group dialog only if are joined to it.
Retrieve list of dialogs
It’s common to request all your dialogs on every app login. The request below will return private, group, and public dialogs that have been updated during the last month, sorted by the last_message_date_sent
in descending order, and limited to 10 dialogs per page.
Argument | Required | Description |
---|---|---|
responsePage | no | If you want to get a paginated list of users from the server, you can set the following fields of the responsePage:- skip allows to skip N records in search results. Default (if not specified): 0.- limit allows limit search results to N records. Default value: 100. |
extendedRequest | yes | A dictionary that stores keys and values of the String type. The keys are formed as parameters of the List Dialogs request. |
If you want to retrieve only dialogs updated after some specific date time and order the search results, you can apply operators. This is useful if you cache dialogs somehow and do not want to obtain the whole list of dialogs on every app start. Thus, you can apply search and sort operators to list dialogs on the page so that it is easier to view specific dialogs. The operators are set as key-value parameters in the extendedRequest
dictionary.
Search operators
You can use search operators to get more specific search results. The request below will return 10 dialogs that were updated over the last month.
Here are the search operators that you can use to search for the exact data that you need.
Search operators | Applicable to types | Applicable to fields | Description |
---|---|---|---|
lt | number, string, date | last_message_date_sent, created_at, updated_at | Less Than operator. |
lte | number, string, date | last_message_date_sent, created_at, updated_at | Less Than or Equal to operator. |
gt | number, string, date | last_message_date_sent, created_at, updated_at | Greater Than operator. |
gte | number, string, date | last_message_date_sent, created_at, updated_at | Greater Than or Equal to operator. |
ne | number, string, date | _id, name, last_message_date_sent | Not Equal to operator. |
in | number, string, date | type, last_message_date_sent, name | IN array operator. |
nin | number, string, date | last_message_date_sent | IN array operator. |
all | number, string, date | last_message_date_sent | ALL are contained in array. |
ctn | number, string, date | name | All records that contain a particular substring. |
Sort operators
You can use sort operators to order the search results. The request below will return dialogs sorted in descending order by the last_message_date_sent
field.
Here are the sort operators that you can use to order the search results:
Sort operator | Applicable to types | Applicable to fields | Description |
---|---|---|---|
sort_asc | All types | id, created_at, name, last_message_date_sent | Search results will be sorted in ascending order by the specified field. |
sort_desc | All types | id, created_at, name, last_message_date_sent | Search results will be sorted in descending order by the specified field. |
Update dialog
You can update the information for a private, group, and public dialog.
Let’s see what capabilities a particular user role has with regard to the dialog type.
Capabilities | Public dialog | Group dialog | Private dialog |
---|---|---|---|
Update a dialog name | Owner | Owner | ✗ |
Update a photo | Owner | Owner | ✗ |
Update custom parameters | Owner,Occupant | Owner,Occupant | Owner,Occupant |
Add occupants
Set pushOccupantsIDs
field to add occupants to the group dialog. As a result, the user ID will be added to the occupantIDs
array.
Parameters | Required | Description |
---|---|---|
chatDialog | Yes | Specifies chatDialog fields that should be set. |
Let’s see what capabilities a particular user role has with regard to the dialog type.
Capabilities | Public dialog | Group dialog | Private dialog |
---|---|---|---|
Add other users | ✗ | Owner,Occupant | ✗ |
Remove occupants
Set the pullOccupantsIDs
field to remove occupants from the group dialog. As a result, the user ID will be removed from the occupantIDs
array.
Parameters | Required | Description |
---|---|---|
chatDialog | Yes | Specifies chatDialog fields that should be set. |
Let’s see what capabilities a particular user role has with regard to the dialog type.
Capabilities | Public dialog | Group dialog | Private dialog |
---|---|---|---|
Remove other users | ✗ | Owner | ✗ |
Remove yourself | ✗ | Owner,Occupant | ✗ |
Delete dialog
A request below will remove a dialog for a current user, but other users will be still able to chat there.
Set the forAllUsers
parameter as true
to completely remove the dialog for all users. You can also delete multiple dialogs in a single request.
Let’s see what capabilities a particular user role has with regard to the dialog type.
Capabilities | Public | Group | Private |
---|---|---|---|
Delete a dialog for all usersusing the forAllUsersparameter | Owner | Owner | Owner |
Delete a dialog for a current user | Owner | Owner,Occupant | Owner,Occupant |
Get number of dialogs
You can get a number of dialogs using the countOfDialogs(withExtendedRequest:)
method. The request below will return a count of dialogs updated over the last month.
Argument | Required | Description |
---|---|---|
extendedRequest | yes | A dictionary that stores keys and values of the String type. The keys are formed as patameters of the List Dialogs request. |
Get number of unread messages
To get a number of unread messages from a particular dialog, use the code snippet below.
You can also retrieve the total number of unread messages using the totalUnreadMessageCountForDialogs()
method . It returns the total count of unread messages for all dialogs of the user in totalUnreadCount
. It also returns unread messages count for dialogs with IDs in dialogsDictionary
if any dialogIDs
are specified.
Argument | Required | Description |
---|---|---|
dialogIDs | yes | IDs of dialogs.- If dialogIDs are not specified, the total number of unread messages for all dialogs of the user will be returned.- If dialogIDs are specified, the number of unread messages for each specified dialog will be returned. Also, the total number of unread messages for all dialogs of the user will be returned. |
Resources
A sequence of steps a user takes to start a dialog by moving through the application lifecycle.
Was this page helpful?