QuickBlox Documentation

QuickBlox provides powerful Chat API and SDK to add real-time messaging and video calls to your web and mobile apps. Learn how to integrate QuickBlox across multiple platforms.

Dialogs

Learn how to create and manage dialogs.

Before you begin

  1. 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.
  2. Configure QuickBlox SDK for your app. Check out our Setup page for more details.
  3. Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.
  4. 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 added 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 QB.chat.DIALOG_TYPE.CHAT dialog type and ID of an opponent you want to create a chat with.

const createDialogParam = {
  type: QB.chat.DIALOG_TYPE.CHAT,
  occupantsIds: [12345]
};

QB.chat
  .createDialog(createDialogParam)
  .then(function (dialog) {
    // handle as neccessary, i.e.
    // subscribe to chat events, typing events, etc.
  })
  .catch(function (e) {
    // handle error
  });

To create a group dialog for a predefined number of occupants, you need to set the QB.chat.DIALOG_TYPE.GROUP_CHAT dialog type and IDs of opponents you want to create a chat with.

const createDialogParam = {
  type: QB.chat.DIALOG_TYPE.GROUP_CHAT,
  name: 'Group Chat',
  occupantsIds: [12345, 12346, 12347]
};

QB.chat
  .createDialog(createDialogParam)
  .then(function (dialog) {
    // handle as neccessary, i.e.
    // subscribe to chat events, typing events, etc.
  })
  .catch(function (e) {
    // handle error
  });

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 just need to set the QB.chat.DIALOG_TYPE.PUBLIC_CHAT dialog type to create a public dialog.

const createDialogParam = {
  type: QB.chat.DIALOG_TYPE.PUBLIC_CHAT,
  name: 'Awesome Public Chat'
};

QB.chat
  .createDialog(createDialogParam)
  .then(function (dialog) {
    // handle as neccessary, i.e.
    // subscribe to chat events, typing events, etc.
  })
  .catch(function (e) {
    // handle error
  });

Join dialog

Before you start chatting in a group or public dialog, you need to join it by calling the joinDialog() 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.

const joinDialogParam = { dialogId: 'dsfsd934329hjhkda98793j2' };

QB.chat
  .joinDialog(joinDialogParam)
  .then(function () { /* joined successfully */ })
  .catch(function (e) { /* handle error */ })

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 occupantsIds array, in the dialog model.

Your user ID is added to the occupantsIds 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.

Leave dialog

You can leave the group and public dialog by calling the leaveDialog() method. If the dialog is left, you can't send/receive messages. To be able to receive/send messages, you need to join it.

const leaveDialogParam = { dialogId: 'dsfsd934329hjhkda98793j2' };

QB.chat
  .leaveDialog(leaveDialogParam)
  .then(function () { /* left successfully */ })
  .catch(function (e) { /* handle error */ })

Let's see, how the leaveDialog() method is used with regard to the dialog type.

Capabilities

Public

Group

Private

Leave

📘

When a group dialog is left, your user ID is removed occupantsIds array, in the dialog model. As a result, the dialog is removed from the list of dialogs and you won't have access to the chat history.

To remove a dialog for all users, use the deleteDialog() method. See this section to learn how to delete the dialog completely for all users.

Retrieve list of dialogs

It's common to request all your dialogs on every app login. The request below will return all private, group, and public dialogs you participate in.

QB.chat
  .getDialogs()
  .then(function (result) {
    // result.dialogs - array of dialogs found
    // result.skip - number of items skipped
    // result.limit - number of items returned per page
    // result.total - total amount of items
  })
  .catch(function (e) {
    // handle error
  });

Filters

If you want to retrieve only dialogs updated after some specific date time, you can pass the 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.

You can apply filters for the following fields:

  • _id (string)
  • type (integer)
  • name (string)
  • last_message_date_sent (integer)
  • created_at (date)
  • updated_at (date)

You can apply sort for the following fields:

  • last_message_date_sent

Filter

Applicable to types

Description

{field_name}

All types

Search records with the field which contains the specified value.

{field_name}[{search_operator}]

All types

Search a record with the field which contains the value according to the specified value and operator.

sort_asc

All types

Search results will be sorted by the specified field in ascending order.

sort_desc

All types

Search results will be sorted by the specified field in descending order.

skip

Integer

Skip N records in search results. Useful for pagination. Default (if not specified): 0.

limit

Integer

Limit search results to N records. Useful for pagination. Default value: 100. If limit is equal to 1, only the last record will be returned.

count

Integer

Count search results. Response will contain only count of records found.

Search operators

Here are the search operators that you can use to retrieve dialogs.

Search operators

Applicable to types

Description

lt

Integer, Float

Less Than operator.

lte

Integer, Float

Less Than or Equal to operator.

gt

Integer, Float

Greater Than operator.

gte

Integer, Float

Greater Than or Equal to operator.

ne

Integer, Float, String, Boolean

Not Equal to operator.

in

Integer, Float, String

IN array operator.

nin

Integer, Float, String

Not IN array operator.

all

Array

ALL are contained in array.

ctn

String

All records that contain a particular substring.

Update dialog

You can update the information for a private, group, and public dialog.

const updateDialogParam = {
  dialogId: 'dsfsd934329hjhkda98793j2',
  name: 'Team room'
};

QB.chat
  .updateDialog(updateDialogParam)
  .then(function (updatedDialog) {
    // handle as necessary
  })
  .catch(function (e) {
    // handle error
  });

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

Add occupants

Set the addUsers field to add occupants to the dialog. As a result, the occupant ID will be added to the occupantsIds array.

const updateDialogParam = {
  dialogId: 'dsfsd934329hjhkda98793j2',
  addUsers: [12340],
};

QB.chat
  .updateDialog(updateDialogParam)
  .then(function (updatedDialog) {
    // handle as necessary
  })
  .catch(function (e) {
    // handle error
  });

Argument

Required

Description

updateDialogParam

yes

Specifies updateDialogParam object with properties that should be updated.

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 removeUsers field remove occupants from a group dialog. As a result, the occupant ID will be removed from the occupantsIds array.

const updateDialogParam = {
  dialogId: 'dsfsd934329hjhkda98793j2',
  removeUsers: [12345],
};

QB.chat
  .updateDialog(updateDialogParam)
  .then(function (updatedDialog) {
    // handle as necessary
  })
  .catch(function (e) {
    // handle error
  });

Argument

Required

Description

updateDialogParam

yes

Specifies updateDialogParam object with properties that should be updated.

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,
Occupant

Remove yourself

Owner,
Occupant

Delete dialog

Use the deleteDialog() method to delete a dialog for all users. When deleting a group dialog, all user IDs will be removed from the occupantsIds array in the dialog model. You can also delete multiple dialogs in a single request.

To delete a dialog for yourself, just leave the dialog. See this section for more information.

const deleteDialogParam = { dialogId: 'dsfsd934329hjhkda98793j2' };

QB.chat
  .deleteDialog(deleteDialogParam)
  .then(function () {
    // dialog was removed successfully
  })
  .catch(function (e) {
    // handle error
  });

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 users

Owner

Owner

Owner,
Occupant

Updated 4 days ago


What's Next

Messaging

Dialogs


Learn how to create and manage dialogs.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.