---
source_url: https://www.pubnub.com/docs/sdks/angular2/api-reference/presence
title: Presence API for Angular2 SDK
updated_at: 2026-05-21T15:45:56.843Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Presence API for Angular2 SDK

Presence lets you track who is online or offline and store custom state information. Presence shows:

* When a user has joined or left a channel
* Who, and how many, users are subscribed to a particular channel
* Which channel(s) an individual user is subscribed to
* Associated state information for these users

Learn more about our Presence feature in the [Presence overview](https://www.pubnub.com/docs/general/presence/overview).

## Here now

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.

:::note Cache
This method has a 3-second response cache time.
:::

### Method(s)

Use the following method in the Angular2 SDK:

```javascript
hereNow( {Array channels, Array channelGroups, Boolean includeUUIDs, Boolean includeState}, Function callback )
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| channels | Array | Optional |  | Specifies the `channel` name to return occupancy results. If `channel` is not provided, `hereNow()` will return data for all `channels`. |
| channelGroups | Array | Optional |  | The `channel group` for which here now information should be received. |
| includeUUIDs | Boolean | Optional | `true` | Setting `uuid` to `false` disables the return of uuids. |
| includeState | Boolean | Optional | `false` | Setting state to `true` enables the return of subscriber state information. |
| callback | Function | Optional |  | Executes on a successful/unsuccessful `hereNow`. |

### Sample code

#### Get a list of uuids subscribed to channel

```javascript
pubnub.hereNow(
    {
        channels: ["my_channel"],
        channelGroups : ["my_channelGroup"],
        includeUUIDs: true,
        includeState: true
    },
    function (status, response) {
        console.log(status);
        console.log(response);
    }
);
```

### Response

```javascript
// Example of Status
{
    error: false,
    operation: "PNHereNowOperation",
    statusCode: 200
}

// Example of Response
{
    totalChannels: 1,
    totalOccupancy: 3,
    channels: {
        myChannel1: {
            occupants: [
                {
                    uuid: "User 524"
                },
                {
                    state: {
                        age: 18
                    },
                    uuid: "User 270"
                },
                {
                    state: {
                        age: 24
                    },
                    uuid: "User 594"
                }
            ],
            name: "my_channel",
            occupancy: 3
        }
    }
}
```

### Other examples

#### Returning state

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

```javascript
pubnub.hereNow(
    {
        channels: ["my_channel"],
        includeState: true
    },
    function (status, response) {
        // handle status, response
    }
);
```

#### Example response

```javascript
// Example of Status
{
    "error": false,
    "operation": "PNHereNowOperation",
    "statusCode": 200
}

// Example of Response
{
    "totalChannels": 1,
    "totalOccupancy": 3,
    "channels": {
        "my_channel": {
            "occupants": [
                {
                    "uuid": "User 1"
                },
                {
                    "state": {
                        "age": 18
                    },
                    "uuid": "User 2"
                },
                {
                    "state": {
                        "age": 24
                    },
                    "uuid": "User 3"
                }
            ],
            "name": "my_channel",
            "occupancy": 3
        }
    }
}
```

#### Return occupancy only

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

To return only `occupancy` for a channel, set `includeUUIDs` and `includeState` to `false`:

```javascript
pubnub.hereNow(
    {
        channels: ["my_channel"],
        includeUUIDs: false,
        includeState: false
    },
    function (status, response) {
        // handle status, response
    }
);
```

#### Example response

```javascript
// Example of Status
{
    "error": false,
    "operation": "PNHereNowOperation",
    "statusCode": 200
}

// Example of Response
{
    "totalChannels": 1,
    "totalOccupancy": 3,
    "channels": {
        "my_channel": {
            "occupants": [],
            "name": "my_channel",
            "occupancy": 3
        }
    }
}
```

#### Channel group usage

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

```javascript
pubnub.hereNow(
    {
        channelGroups : ["my_channelGroup"]
    },
    function (status, response) {
        // handle status, response
    }
);
```

#### Example response

```javascript
// Example of Status
{
    "error": false,
    "operation": "PNHereNowOperation",
    "statusCode": 200
}

// Example of Response
{
    "totalChannels": 2,
    "totalOccupancy": 3,
    "channels": {
        "my_channel_1": {
            "occupants": [
                {
                    "state": null,
                    "uuid": "User1"
                },
                {
                    "state": null,
                    "uuid": "User3"
                }
            ],
            "name": "my_channel_1",
            "occupancy": 2
        },
        "my_channel_2": {
            "occupants": [
                {
                    "state": null,
                    "uuid": "User2"
                }
            ],
            "name": "my_channel_2",
            "occupancy": 1
        }
    }
}
```

## Where now

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

This method returns the list of channels a UUID is subscribed to.

:::note
If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.
:::

### Method(s)

Use the following method in the Angular2 SDK:

```javascript
whereNow ({String uuid},Function callback)
```

| Parameter | Description |
| --- | --- |
| `uuid`Type: StringDefault: `current uuid` | Specifies the `uuid` to return channel list for. |
| `callback`Type: FunctionDefault: n/a | Executes on a successful/unsuccessful `whereNow`. |

### Sample code

You simply need to define the `uuid` and the `callback` function to be used to send the data to as in the example below.

#### Get a list of channels a UUID is subscribed to

```javascript
pubnub.whereNow(
    {
        uuid: 'my_uuid'
    },
    function (status, response) {
        // handle status, response
    }
);
```

### Response

```javascript
// Example of Status
{
    error: false,
    operation: "PNWhereNowOperation",
    statusCode: 200
}

// Example of Response
{
    "channels": ["ch1", "ch2"]
}
```

### Other examples

#### Sample code using promises

```javascript
pubnub.whereNow({
    uuid: 'my_uuid'
}).then((response) => {
    console.log(response);
}).catch((error) => {
    console.log(error);
});
```

## User state

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

Clients can set a dynamic custom state (score, game state, location) for their users on one or more channels and store it on a channel as long as the user stays subscribed.

The state is not persisted, and when the client disconnects, the state data is lost. For more information, refer to [Presence State](https://www.pubnub.com/docs/general/presence/presence-state).

### Method(s)

#### Set state

```javascript
setState({Array channels, Array channelGroups, Object state},Function callback)
```

| Parameter | Description |
| --- | --- |
| `channels`Type: Array | Either `channels` or `channelGroups` should be provided, Specifies the `channels` to set the state. |
| `channelGroups`Type: Array | Either `channels` or `channelGroups` should be provided, Specifies the Channel Group to set the state |
| `state`Type: Object | JSON object of key/value pairs with supported data-types of int, float and string. Nesting of key/values is not permitted and key names beginning with prefix `pn` are reserved. If the state parameter is undefined, the current state for the specified `uuid` will be returned. If a specified key already exists for the `uuid` it will be over-written with the new value. Key values can be deleted by setting the particular value to `null`. |
| `callback`Type: Function | Executes on a successful/unsuccessful `setState`. |

#### Get state

```javascript
getState({String uuid, Arraychannels, ArraychannelGroups},Function callback)
```

| Parameter | Description |
| --- | --- |
| `uuid`Type: StringDefault: `current uuid` | The subscriber `uuid` to get the current state. |
| `channels`Type: ArrayDefault: n/a | Either `channels` or `channelGroups` should be provided, Specifies the `channels` to get the state. |
| `channelGroups`Type: ArrayDefault: n/a | Either `channels` or `channelGroups` should be provided, Specifies the Channel Group to get the state. |
| `callback`Type: FunctionDefault: n/a | Executes on a successful/unsuccessful `getState`. |

### Sample code

#### Set state

```javascript
pubnub.setState(
    {
        state: {
            "Key": "Value"
        },
        channels: ["my_channel"]
    },
    function (status, response) {
        // handle status, response
    }
);
```

#### Get state

```javascript
pubnub.getState(
    {
        uuid : "my_uuid",
        channels : ["my_channel"]
    },
    function (status, response) {
        // handle status, response
    }
);
```

### Response

#### Set state

```javascript
// Example of Status
{
    error: false,
    operation: "PNSetStateOperation",
    statusCode: 200
}

// Example of Response
{
    state: {
        me: 'typing'
    }
}
```

#### Get state

```javascript
// Example of Status
{
    error: false,
    operation: "PNGetStateOperation",
    statusCode: 200
}

// Example of Response
{
    channels: {
        ch1: {
            me: 'typing'
        }
    }
}
```

### Other examples

#### Set state Basic usage using promises*

```javascript
pubnub.setState({
    state: {
        "Key": "Value"
    },
    channels: ["my_channel"]
}).then((response) => {
    console.log(response);
}).catch((error) => {
    console.log(error);
});
```

#### Get state Basic usage using promises

```javascript
pubnub.getState({
    uuid : "my_uuid",
    channels : ["my_channel"]
}).then((response) => {
    console.log(response);
}).catch((error) => {
    console.log(error);
});
```