> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quickblox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# User Presence

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

## Before you begin

1. Register a [QuickBlox account](https://admin.quickblox.com/signin). 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 [Setup](/sdks/js-setup) page for more details.
3. Create a user session to be able to use QuickBlox functionality. See [Authentication](/sdks/js-authentication) page to learn how to do it.
4. Connect to the Chat server. See [Connection](/sdks/js-chat-connection) page to learn how to do it.

Visit [Key Concepts](/docs/key-concepts) page to learn 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](/sdks/js-chat-contact-list) to learn how to implement the Contact list.

```JavaScript JavaScript theme={null}
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**

```JavaScript JavaScript theme={null}
// 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
  }
}
```

| Argument   | Required | Description                                                                                     |
| ---------- | -------- | ----------------------------------------------------------------------------------------------- |
| userId     | yes      | ID of the user.                                                                                 |
| function() | yes      | Specifies a callback function that accepts one argument that specifies what error has happened. |

**Ping user by JID**

```JavaScript JavaScript theme={null}
// 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
  }
}
```

| Argument   | Required | Description                                                                                                                                                                                                                                                      |
| ---------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| userJid    | yes      | User 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() | yes      | Specifies 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.

```JavaScript JavaScript theme={null}
// 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
  }
}
```

| Argument   | Required | Description                                                                                                                                                                                                                                                                                |
| ---------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| server     | no       | Custom 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](/sdks/js-setup#point-sdk-to-enterprise-server) section to learn how to set up custom server endpoints. |
| function() | yes      | Specifies 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 [Initialize SDK](/sdks/js-setup#initialize-quickblox-sdk) section to learn more about other configuration options.

```JavaScript JavaScript theme={null}
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);
```
