PubNub App Context
App Context lets you store and manage data (also called metadata) for users, channels, and their relationships (memberships) directly on the PubNub platform.
You deliver personalized, context‑rich experiences in real time without adding external databases. This improves user experience and keeps data secure and compliant.
Configuration
Enable and configure App Context on a keyset in the Admin Portal.
Public Admin Portal demo
Want to browse through the Admin Portal without creating an account? Explore it through the Public Demo that shows examples of most PubNub features for transport and logistics use case.
By default, the Admin Portal prompts you to enable App Context on every new keyset (unless you choose Choose later for Region during keyset creation).
Testing keysets default to the US East
region for storing user, channel, and membership data.
Option | Description |
---|---|
Bucket Region | The geographic region where your App Context data is stored and managed. Choose a region close to your users to reduce latency and improve performance. After you save, you cannot change the region. |
User Metadata Events | Turns on events when user metadata is created, updated, or deleted. Apps can subscribe to these events and react in real time. See Real-time updates to learn event types and how to receive them. |
Channel Metadata Events | Turns on events when channel metadata is created, updated, or deleted. Apps can subscribe and update UI based on channel property changes. See Real-time updates to learn event types and how to receive them. |
Membership Events | Turns on events for memberships (associations between users and channels). This helps you manage and respond to who belongs to which channels. Subscribe to receive and handle these events. See Real-time updates to learn event types and how to receive them. |
Disallow Get All Channel Metadata | Restricts access to all channel metadata in apps with Access Manager enabled. If you enable Access Manager and leave this unchecked, you can get all channel metadata without listing it in the token’s permission schema. |
Disallow Get All User Metadata | Restricts access to all user metadata in apps with Access Manager enabled. If you enable Access Manager and leave this unchecked, you can get all user metadata without listing it in the token’s permission schema. |
Enforce referential integrity for memberships | When enabled: you can create a membership only if the specified user ID and channel ID exist as standalone metadata entities. Deleting a user or channel automatically deletes its child membership associations. When disabled: you can create memberships even if the user or channel entity doesn’t exist. Deleting a user or channel doesn’t delete associated memberships. |
Functional components
App Context includes three metadata types:
-
Channel metadata — Maintain channel data (names, descriptions, custom attributes) that support how each channel works in your app.
-
User metadata — Store and manage user data (name, email, custom attributes) using App Context’s serverless storage.
-
Membership metadata — Manage relationships between users and channels to add, update, or remove users from channels and vice versa.
Data management
You can manage App Context data using SDK methods, REST API endpoints, or the BizOps Workspace GUI. Choose the method that fits your workflow.
SDKs
SDKs are best for real‑time, in‑app interactions. PubNub provides SDKs for many languages and platforms so you can manage App Context programmatically.
In each SDK, the left navigation includes an App Context section with methods. For reference, see JavaScript.
REST API
Use the REST API for server‑to‑server interactions or when an SDK isn’t available.
PubNub’s REST API lets you manage App Context server‑side or from any platform that can make HTTP requests.
BizOps Workspace
BizOps Workspace is ideal for operational teams that prefer a GUI to manage users, channels, and memberships.
BizOps Workspace in the Admin Portal includes these views:
- User management — Create, update, and delete user and membership data.
- Channel management — Create, update, and delete channel and membership data.
Real-time updates
PubNub generates events when user, channel, or membership metadata changes (create, update, delete). Clients can subscribe to these events to react in real time and update the UI.
App Context events
PubNub defines the following App Context events:
Event | Type | When it's fired |
---|---|---|
User Metadata Set | User | Metadata is set for a user |
User Metadata Deleted | User | Metadata is deleted for a user |
Channel Metadata Set | Channel | Metadata is set for a channel |
Channel Metadata Deleted | Channel | Metadata is deleted for a channel |
User Added to Channel | Membership | A membership record is created or updated between a user and a channel |
User Removed from Channel | Membership | A membership record is removed between a user and a channel |
All App Context events use the following format:
{
"channel": "sample-channel",
// event payload
"message": {
// API from which data is originating
"source": "objects",
// event structure version
"version": "2.0",
// type of occurring event, e.g. "set" or "delete"
"event": "sample-event",
// category of data, e.g. "membership" or "channel"
"type": "sample-type",
// specific event-related data
"data": {
// e.g. "type": {member.Type},
show all 24 linesThe fields are defined as follows:
- event — What happened (
set
ordelete
). - type — Where it happened (
uuid
for user,channel
, ormembership
). - data — Event‑specific information.
User metadata event notifications
When the User Metadata Events option is enabled on a keyset, any modification to a user entity (set
and delete
) sends notifications to that user and to any channels the user belongs to.
For example, if a user with User ID test-user-1
is a member of channels test-channel-1
and test-channel-2
, and that user entity is modified, a set
event for type uuid
is published to three channels: test-user-1
(user‑associated channel), test-channel-1
, and test-channel-2
. A delete
event is published to those three channels if that User ID is deleted.
Subscribe to App Context events
To receive App Context events, use an SDK to listen for real‑time metadata changes to users, channels, or memberships.
Detailed steps live in each SDK. In general:
-
Initialize PubNub — Set up your instance with your publish and subscribe keys.
-
Add a listener — Implement an event listener to handle App Context events (user, channel, memberships). See Receive.
-
Subscribe to channels — Choose channels or patterns that receive App Context events and subscribe.
You can subscribe to a channel (to receive all membership info from that channel) or to a user (to receive changes to that user or their memberships). Subscribing to a user means subscribing to that user’s user‑ID channel.
You can also use PubNub Functions to consume, modify, and analyze messages or events in real time with JavaScript that runs on the PubNub network.
API limits
PubNub applies soft and hard limits for App Context (record sizes, field lengths, and recommended maximums for memberships and members). For details, see API Limits.