Connection

Learn how to connect to the chat server and set connection settings.

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.

Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.

Connect to Chat server

To connect to Chat server use the code snippet below.

var userCredentials = {
  userId: 4448514,
  password: "awesomepwd"
};

QB.chat.connect(userCredentials, function(error, contactList) {});

Connect to chat server with QuickBlox session token

In case, you authenticate with QuickBlox via Firebase, Facebook, or Custom Identity Provider, you should connect to the chat server with QuickBlox user session token and QuickBlox user ID.

As a result of successful authentication via Firebase, Facebook, or Custom Identity Provider, you receive a QuickBlox user session token and QuickBlox user ID. Use QuickBlox user session token as a password and QuickBlox user ID as a login to connect to the chat server.

🚧

Don't use the Firebase/Facebook access token as a password to connect to the chat server. You will receive the following runtime error in this case: <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/><text xml:lang='en'>Password not verified</text></failure>.

var session = QB.service.getSession();
var userId = session.user_id;
var password = session.token;
var params = {userId, password};

QB.chat.connect(params, function(error, contactList) {});

The connect() method accepts one argument of the object type that has the following fields:

Field

Required

Description

userId

yes

ID of the user.

password

yes

Specifies an active QuickBlox user session token set as a password.

Disconnect from Chat server

Disconnect from the Chat server using the snippet below.

QB.chat.disconnect();

QB.chat.onDisconnectedListener = onDisconnectedListener;

function onDisconnectedListener() {
  console.log("onDisconnected");
}

Reconnection

QuickBlox Chat runs over XMPP protocol. To receive messages in a real-time mode, the application should be connected to the Chat over XMPP protocol. The SDK can be reconnected to the Chat server when the connection is lost. The following 2 callbacks are used to track the state of the connection.

QB.chat.onDisconnectedListener = onDisconnectedListener;
QB.chat.onReconnectListener = onReconnectListener;

function onDisconnectedListener() {}

function onReconnectListener() {}

What’s Next
Did this page help you?