---
source_url: https://www.pubnub.com/docs/general/setup/users-and-devices
title: Users & Devices
updated_at: 2026-05-22T11:05:26.573Z
---

> 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


# Users & Devices

All clients have a notion of a user, a device, or both. Uniquely identifying each one is essential for how they interact with the PubNub platform. Clients are entities that connect through your app. In most apps, users are people who connect from the app.

The relationship between users and devices matters. One user can use your app on multiple devices. Decide whether your app allows simultaneous multi‑device use. That choice determines how you identify users and devices.

:::note User ID / UUID
User ID is also referred to as **UUID/uuid** in some APIs and server responses but **holds the value** of the **userId** parameter you [set during initialization](https://www.pubnub.com/docs/general/setup/users-and-devices#set-the-user-id).
:::

## User ID usage

A User ID is a UTF‑8 encoded, unique string of up to 92 characters used to identify a single client (end user, device, or server) that connects to PubNub. User IDs are also important for features like Presence and Message Persistence.

:::warning User ID is required
[Set](#set-the-user-id) the User ID to connect to PubNub and ensure accurate billing.
:::

User IDs are kept lean by design. Use [User metadata](https://www.pubnub.com/docs/general/metadata/users-metadata) or [dynamic User state](https://www.pubnub.com/docs/general/presence/presence-state) (temporary while connected) for additional data.

###### Extend your User ID with persistent metadata

App Context stores structured metadata (name, email, profile URL, and custom fields) keyed to your User ID. Unlike dynamic Presence state that clears on disconnect, App Context metadata persists across sessions. PubNub fires real-time events whenever a user's metadata changes, delivered to channels the user belongs to. See [User Metadata](https://www.pubnub.com/docs/general/metadata/users-metadata) for storage operations and [App Context events](https://www.pubnub.com/docs/general/metadata/basics#real-time-updates) for event types.

Generate the User ID once per user or device. Reuse it for their lifetime. Pass it to the client after login, or persist it so the next PubNub instance can reuse it. In SDKs, parameter names such as `userId`, `user_id`, and `uuid` refer to the same User ID value.

:::note User Identification Confidentiality
The User ID may be visible to other clients. Don't use usernames, emails, or other personally identifiable or confidential information. Choose a value you can revoke and replace without user action.
:::

### Device-level presence tracking

Multiple devices that share the same User ID count as one user for [billing](https://www.pubnub.com/docs/general/setup/account-setup#pricing-model). If one device unsubscribes while others stay subscribed, Presence can emit a `leave` event followed by a `join` event.

To address this, consider:

* Assign a different identifier to each device.
* Unsubscribe from channels only if no other devices stay subscribed.
* If disconnects without unsubscribe are acceptable, rely on the `timeout` presence event instead.

## Set the user ID

For servers, read the User ID from a server config file. For clients, receive the User ID from your server after login.

### JavaScript

```javascript
<script type="text/javascript">
  var pubnub = new PubNub({
    publishKey: "myPublishKey",
    subscribeKey: "mySubscribeKey",
    userId: "myUniqueUserId"
});
<script>
```

### Python

```python
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

pnconfig = PNConfiguration()
pnconfig.publish_key = "myPublishKey"
pnconfig.subscribe_key = "mySubscribeKey"
pnconfig.user_id = "myUniqueUserId"

pubnub = PubNub(pnconfig)
```

### Java

```java
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("yourUserId"), "yourSubscribeKey");
// publishKey from Admin Portal (only required if publishing)
configBuilder.publishKey("PublishKey");
PubNub pubNub = PubNub.create(configBuilder.build());
```

### Kotlin

```kotlin
val config = PNConfiguration(UserId("myUniqueUserId")).apply {
      publishKey = "myPublishKey"
      subscribeKey = "mySubscribeKey"
  }
  var pubnub = PubNub(config)
```

### Go

```go
pnconfig := pubnub.NewConfig()
pnconfig.SubscribeKey = "MySubscribeKey"
pnconfig.PublishKey = "MyPublishKey"
pnconfig.SetUserId(UserId("myUniqueUserId"))
pn := pubnub.NewPubNub(pnconfig)
```

### C

```c
pubnub_t *ctx = pubnub_alloc();
if (NULL == ctx) {
    puts("Couldn't allocate a Pubnub context");
    return -1;
}
pubnub_init(ctx, "MyPublishKey", "MySubscribeKey");
pubnub_set_user_id(ctx, "myUniqueUser_id");
```

### Unity

```csharp
using PubNubAPI;

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.UserId = "myUniqueUserId";
pubnub = new PubNub(pnConfiguration);
```

### Swift

```swift
import PubNubSDK

let config = PubNubConfiguration(
  publishKey: "demo",
  subscribeKey: "demo",
  userId: "myUniqueUserId"
)

let pubnub = PubNub(configuration: config)
```

### Objective-C

```objectivec
#import <PubNub/PubNub.h>

PNConfiguration *pnconfig = [PNConfiguration configurationWithPublishKey:@"myPublishKey"
                                             subscribeKey:@"mySubscribeKey"];
pnconfig.uuid = "theClientUUID";
PubNub *pubnub = [PubNub clientWithConfiguration:pnconfig];
```

### C#

```csharp
using PubnubApi;

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.UserId = "myUniqueUserId";
pubnub = new PubNub(pnConfiguration);
```

### Dart

```dart
final myKeyset = Keyset(
  subscribeKey: 'mySubscribeKey',
  publishKey: 'myPublishKey',
  userId: UserId('yourUniqueUserId')
  );
```

### PHP

```php
use PubNub\PNConfiguration;
use PubNub\PubNub;

$pnconf = new PNConfiguration();

$pnconf->setSubscribeKey("my-key");
$pnconf->setPublishKey("my-key");
$pnconf->setUserId("myUniqueUserId");
$pubnub = new PubNub($pnconf);
```

### Ruby

```ruby
pubnub = Pubnub.new(
    subscribe_key: :demo,
    publish_key:   :demo,
    user_id: 'myUniqueUserId'
)
```

### Multiple devices per user

Decide whether a user can use your app on two or more devices at the same time. For example, a user might use chat in a browser and then switch to a phone. Users can pass device information as dynamic state if they want to share it with others in the app.

Users can connect from multiple devices using the same User ID. They can pass device info with the Set State operation. Dynamic state is kept in memory while connected and clears on disconnect.

If a user connects from multiple devices at once, they receive the same messages on each device. Presence status may be inaccurate in this case.

### Server generates user ID for the client

Most often, you generate a User ID on your server when the user registers. Store it with the user’s profile. Reuse it to associate the user with PubNub.

When the user logs in, pass the User ID to the client. Use it during PubNub initialization.

### Server generates user ID for its own use

If the server interacts with PubNub, each server instance should set and reuse its User ID. Include the User ID in your server config with your PubNub API keys.

## User ID impact

Reuse a User ID for each user to optimize usage metrics, billing, and Presence.

### Billing

If you use the Monthly Active Users (MAU) pricing model, creating a new User ID on each sign‑in can count the same user multiple times in a month. Reuse the same User ID to keep billing accurate.

### Presence

With Presence enabled, a user must be uniquely identifiable. If a user rejoins a channel with a different User ID, they appear as a different subscriber on that channel.

###### Understand how Presence tracks your User ID

Presence uses the User ID to identify each subscriber on a channel. Every join, leave, timeout, and state-change event includes the User ID of the affected client. When multiple devices share one User ID, Presence may emit unexpected leave/join sequences as individual connections drop. See [Presence Basics](https://www.pubnub.com/docs/general/presence/overview) for configuration options and [Presence Events](https://www.pubnub.com/docs/general/presence/presence-events) for event types and listener setup.

### Server user ID usage

If you don’t reuse a User ID for server instances, monthly active user metrics can increase, especially if you create a new PubNub object for each call. Reuse the PubNub object where possible (an instance pool pattern). Always reuse the User ID on servers and clients.

###### Leverage your User ID across platform security and services

Your User ID acts as the identity thread connecting security, delivery, and processing across PubNub. [Access Manager](https://www.pubnub.com/docs/general/security/access-control#authorized-uuid) binds tokens to your User ID so only authenticated clients can access resources, and can scope metadata [permissions](https://www.pubnub.com/docs/general/security/access-control#permissions) per user. [Message Persistence](https://www.pubnub.com/docs/general/storage) records the publisher User ID with every stored message, enabling per-user attribution in history. [File Sharing](https://www.pubnub.com/docs/general/files) carries the publisher identity in file message events, and [Mobile Push Notifications](https://www.pubnub.com/docs/general/push/send#prevent-self-notifications) use the User ID-to-device relationship for self-notification exclusion. [Functions](https://www.pubnub.com/docs/serverless/functions/overview#after-presence-functions) and [Events & Actions](https://www.pubnub.com/docs/serverless/events-and-actions/events#filters) filter or route logic per User ID, while [Illuminate](https://www.pubnub.com/docs/illuminate/business-objects/basics#data-mapping) and [Insights](https://www.pubnub.com/docs/pubnub-insights/dashboards/users) capture and aggregate per-user activity for metrics and decisioning.

## Troubleshooting

When you troubleshoot your app or PubNub Support investigates an issue, tracking a user by User ID across requests and logs is useful.

## Terms in this document

* **Channel** - A pathway for sending and receiving messages between devices, created automatically when you first use it, that can handle any number of users and messages for different communication needs, like 1-1 text chats, group conversations, and other data streaming.
* **Channel pattern** - A way to group and analyze channel data to track performance metrics like message counts and user engagement over time with PubNub Insights.
* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.
* **User** - An individual or entity that interacts with a system, application, or service. In PubNub, a user typically refers to someone who sends or receives messages through the platform, identified by a unique user ID or username.
* **User ID** - UTF-8 encoded, unique string of up to 92 characters used to identify a single client (end user, device, or server) that connects to PubNub.
