User Presence

Learn how to track user presence updates and check user status using ping.

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.

Subscribe to contact presence updates

You can track contact list updates in real-time by using the onContactListListener. However, you can track the presence updates of only those users who have been added to the contact list. See this section to learn how to implement the Contact list.

QB.chat.onContactListListener = function(userId, type) {
  // type - if a user left the chat, type will be 'unavailable'.
  // Otherwise - 'available'.
};

Ping user

QuickBlox SDK can send application-level pings to a user. As a result, you can check if the user is connected to the Chat server.

Ping user by ID

// ping user (by user ID)
try {
  QB.chat.ping(userId, function (error) {
    if (error) {
      // no pong received
    } else {
      // pong received from user
    }
  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
ArgumentRequiredDescription
userIdyesID of the user.
function()yesSpecifies a callback function that accepts one argument that specifies what error has happened.

Ping user by JID

// ping user (by user JID)
try {
  QB.chat.ping(userJid, function (error) {
    if (error) {
      // no pong received
    } else {
      // pong received from user
    }
  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
ArgumentRequiredDescription
userJidyesUser JID. JID (Jabber ID) of the user in the in the XMPP server. Assigned automatically by the server upon the login to the Chat. The users will have different JIDs for different apps.

The JID format is the following:
<user_id>-<app_id>@chat.quickblox.com
function()yesSpecifies a callback function that accepts one argument that specifies what error has happened.

Ping server

QuickBlox SDK can send application-level pings to a server. As a result, you can check if there is a connection with the Chat server.

// ping server (some specific chat server)
var server = 'chat.quickblox.com';
try {
  QB.chat.ping(server, function (error) {
    if (error) {
      // no pong received
    } else {
      // pong received from server
    }
  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
ArgumentRequiredDescription
servernoCustom server address. You can ping a specific chat server. If no server is specified, the server address is taken from the config object. See the Point SDK to enterprise server section to learn how to set up custom server endpoints.
function()yesSpecifies a callback function that accepts one argument that specifies what error has happened.

Set ping timeout

To control how much time it takes to respond to a ping, you should set a pingTimeout. By default, the pingTimeout is 30 seconds. If the response wasn't received within the specified time frame, then the error callback is called.

Set the pingTimeout in seconds, in the CONFIG object. See our Initialize SDK section to learn more about other configuration options.

var APPLICATION_ID = "41";
var AUTH_KEY = "lkjdueksu7392kj";
var AUTH_SECRET = "iiohfdija792hj";
var ACCOUNT_KEY = "sdjfnksnlk2bk1k34kb";
var CONFIG = {
  // other configs
  pingTimeout: 3
};
QB.init(APPLICATION_ID, AUTH_KEY, AUTH_SECRET, ACCOUNT_KEY, CONFIG);

What’s Next