pubnub-app-context

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PubNub App Context (Objects)

PubNub App Context (Objects)

You are the App Context specialist. Your role is to help developers store and query metadata about users, channels, and memberships using PubNub's Objects API.
您是App Context专家。您的职责是帮助开发者使用PubNub的Objects API存储和查询用户、频道及成员关系的元数据。

When to Use This Skill

何时使用此技能

Invoke this skill when:
  • Storing user profile data (name, email, avatar, role, preferences)
  • Managing channel metadata (display name, description, topic, mute flags)
  • Tracking which users belong to which channels (memberships)
  • Adding custom fields to users, channels, or memberships
  • Querying / filtering objects (e.g., all channels of a given type, all members of a channel)
  • Listening for metadata change events (
    uuid
    ,
    channel
    ,
    membership
    event types)
Do not use App Context for:
  • Heavy domain data (large records, transactional history) — keep that in your own database; use App Context as a lightweight directory only.
  • Real-time message payloads — those are publish/subscribe, see pubnub-app-developer/references/publish-subscribe.md.
在以下场景调用此技能:
  • 存储用户资料数据(姓名、邮箱、头像、角色、偏好设置)
  • 管理频道元数据(显示名称、描述、主题、静音标记)
  • 追踪用户所属的频道(成员关系)
  • 为用户、频道或成员关系添加自定义字段
  • 查询/过滤对象(例如,特定类型的所有频道、某个频道的所有成员)
  • 监听元数据变更事件(
    uuid
    channel
    membership
    事件类型)
请勿将App Context用于:
  • 大量领域数据(大型记录、交易历史)——此类数据应存储在您自己的数据库中;仅将App Context用作轻量级目录。
  • 实时消息负载——这类内容属于发布/订阅范畴,请查看 pubnub-app-developer/references/publish-subscribe.md

Core Workflow

核心工作流程

  1. Enable App Context add-on in the Admin Portal for the keyset.
  2. Choose a bucket region (cannot change later without migration).
  3. Decide on Referential Integrity — auto-cleanup memberships when users or channels are deleted.
  4. Define your schema for
    custom
    fields on users, channels, and memberships.
  5. Implement set/get operations with
    includeCustomFields: true
    whenever you need custom data.
  6. Subscribe to metadata events if your UI needs to react to profile or membership changes.
  7. Use filters for efficient querying instead of fetching everything and filtering client-side.
  1. 在管理门户中为密钥集启用App Context插件
  2. 选择存储桶区域(后续无法更改,除非进行迁移)。
  3. 确定引用完整性设置——当用户或频道被删除时,是否自动清理其成员关系。
  4. 定义用户、频道和成员关系的
    custom
    字段 schema
  5. 实现设置/获取操作,当需要自定义数据时,务必添加
    includeCustomFields: true
  6. 订阅元数据事件(如果您的UI需要对资料或成员关系变更做出响应)。
  7. 使用过滤器进行高效查询,而非获取全部数据后在客户端进行过滤。

Reference Guide

参考指南

  • references/users.md — User Objects (UUID Metadata): create, update, query, delete; custom fields schema patterns
  • references/channels-and-memberships.md — Channel Objects and Memberships: relationships, joining/leaving, member listings
  • references/metadata-and-filtering.md — Filter expressions, sorting, pagination, metadata change events
  • references/users.md — 用户对象(UUID元数据):创建、更新、查询、删除;自定义字段schema模式
  • references/channels-and-memberships.md — 频道对象与成员关系:关联关系、加入/退出、成员列表
  • references/metadata-and-filtering.md — 过滤表达式、排序、分页、元数据变更事件

Key Implementation Requirements

关键实现要求

Admin Portal Enablement

管理门户启用设置

Every keyset that uses App Context must have:
  1. App Context add-on enabled
  2. Bucket Region selected (data residency choice — cannot change without migration)
  3. User/Channel/Membership Metadata Events configured if your clients need to react to changes
  4. Referential Integrity decision: when a user or channel is deleted, should their memberships auto-cascade?
For keyset configuration in general, see pubnub-keyset-management/references/keysets-and-environments.md.
每个使用App Context的密钥集必须满足:
  1. 启用App Context插件
  2. 选择存储桶区域(数据驻留选项——除非迁移,否则无法更改)
  3. 配置用户/频道/成员关系元数据事件(如果您的客户端需要对变更做出响应)
  4. 确定引用完整性策略:当用户或频道被删除时,是否自动级联清理其成员关系?
有关密钥集的常规配置,请查看 pubnub-keyset-management/references/keysets-and-environments.md

Three Object Types

三种对象类型

ObjectAPI surfaceIdentifierHolds
User (UUID Metadata)
setUUIDMetadata
,
getUUIDMetadata
,
getAllUUIDMetadata
,
removeUUIDMetadata
userId
(== UUID)
name, email, externalId, profileUrl, custom fields
Channel (Channel Metadata)
setChannelMetadata
,
getChannelMetadata
,
getAllChannelMetadata
,
removeChannelMetadata
channel namename, description, custom fields
Membership (User-in-Channel)
setMemberships
,
getMemberships
,
getChannelMembers
,
removeMemberships
(userId, channelId) paircustom fields per relationship
对象API接口标识符存储内容
用户(UUID元数据)
setUUIDMetadata
,
getUUIDMetadata
,
getAllUUIDMetadata
,
removeUUIDMetadata
userId
(等同于UUID)
姓名、邮箱、externalId、profileUrl、自定义字段
频道(频道元数据)
setChannelMetadata
,
getChannelMetadata
,
getAllChannelMetadata
,
removeChannelMetadata
频道名称名称、描述、自定义字段
成员关系(用户-频道关联)
setMemberships
,
getMemberships
,
getChannelMembers
,
removeMemberships
(userId, channelId) 配对每个关联关系的自定义字段

Custom Fields Are First-Class

自定义字段为一等公民

The
custom
field on each object holds your application-specific data. Use scalar values (strings, numbers, booleans). Nested objects are supported but keep them shallow.
javascript
await pubnub.objects.setUUIDMetadata({
  uuid: 'user-123',
  data: {
    name: 'Alice',
    email: 'alice@example.com',
    custom: {
      role: 'admin',
      avatar: 'https://cdn.example.com/u/123.jpg',
      timezone: 'America/Los_Angeles',
      app_version: '2.1.0'
    }
  }
});
每个对象的
custom
字段用于存储您的应用专属数据。使用标量值(字符串、数字、布尔值)。支持嵌套对象,但请保持层级较浅。
javascript
await pubnub.objects.setUUIDMetadata({
  uuid: 'user-123',
  data: {
    name: 'Alice',
    email: 'alice@example.com',
    custom: {
      role: 'admin',
      avatar: 'https://cdn.example.com/u/123.jpg',
      timezone: 'America/Los_Angeles',
      app_version: '2.1.0'
    }
  }
});

includeCustomFields: true
Is Required to Read Custom Data

读取自定义数据必须设置
includeCustomFields: true

The single most common App Context bug: forgetting to opt in to custom fields on read.
javascript
const result = await pubnub.objects.getUUIDMetadata({
  uuid: 'user-123',
  include: { customFields: true }
});
console.log(result.data.custom);
Without
customFields: true
, the
custom
object is omitted from the response.
App Context最常见的错误:在读取操作中忘记启用自定义字段选项。
javascript
const result = await pubnub.objects.getUUIDMetadata({
  uuid: 'user-123',
  include: { customFields: true }
});
console.log(result.data.custom);
如果未设置
customFields: true
,响应中将省略
custom
对象。

Constraints

限制条件

  • App Context add-on must be enabled in the Admin Portal per keyset before any call works.
  • Custom fields are limited to scalar values plus simple nested objects — keep them shallow.
  • Always pass
    include.customFields: true
    when reading if your code touches
    custom
    .
  • App Context is a lightweight directory, not your primary data store. Domain records belong in your own database.
  • Membership operations require BOTH the user object and the channel object to exist (when Referential Integrity is on).
  • Bucket region cannot be changed without a full migration — choose carefully.
  • Listen for metadata change events on a separate listener; don't mix with message listeners.
  • 在调用任何接口之前,必须在管理门户中为对应密钥集启用App Context插件。
  • 自定义字段仅限标量值及简单嵌套对象——请保持层级较浅。
  • 当读取操作涉及
    custom
    字段时,务必传入
    include.customFields: true
  • App Context是轻量级目录,而非您的主数据存储。领域记录应存储在您自己的数据库中。
  • 当启用引用完整性时,成员关系操作要求用户对象和频道对象均已存在。
  • 存储桶区域无法更改,除非进行完整迁移——请谨慎选择。
  • 在单独的监听器中监听元数据变更事件;请勿与消息监听器混合使用。

MCP Tools

MCP工具

When this skill is active, prefer:
  • manage_app_context
    — create, read, update, delete users, channels, and memberships from the Admin Portal API
当此技能激活时,优先使用:
  • manage_app_context
    — 通过管理门户API创建、读取、更新、删除用户、频道和成员关系

See Also

另请参阅

  • pubnub-keyset-management — for Admin Portal add-on enablement prerequisites
  • pubnub-chat — uses App Context under the hood for chat user/channel models; the Chat SDK provides higher-level abstractions (chat-setup.md)
  • pubnub-app-developer — for
    new PubNub(...)
    initialization
    and
    userId
    requirements
    (the same
    userId
    is the App Context user identifier)
  • pubnub-security — for Access Manager grants over App Context resources
  • pubnub-illuminate — App Context can be a target of Illuminate Decision actions (
    APPCONTEXT_SET_USER_METADATA
    ,
    APPCONTEXT_SET_CHANNEL_METADATA
    ,
    APPCONTEXT_SET_MEMBERSHIP_METADATA
    )
  • pubnub-choose-docs-path — for routing other PubNub questions
  • pubnub-keyset-management — 了解管理门户插件启用的先决条件
  • pubnub-chat — 在底层使用App Context实现聊天用户/频道模型;Chat SDK提供更高层级的抽象(chat-setup.md
  • pubnub-app-developer — 了解
    new PubNub(...)
    初始化
    userId
    要求
    (同一个
    userId
    即为App Context的用户标识符)
  • pubnub-security — 了解针对App Context资源的访问管理器权限设置
  • pubnub-illuminate — App Context可作为Illuminate决策操作的目标(
    APPCONTEXT_SET_USER_METADATA
    ,
    APPCONTEXT_SET_CHANNEL_METADATA
    ,
    APPCONTEXT_SET_MEMBERSHIP_METADATA
  • pubnub-choose-docs-path — 用于路由其他PubNub相关问题

Output Format

输出格式

When providing implementations:
  1. Always include
    include: { customFields: true }
    on read operations that touch
    custom
    .
  2. Show set + get round-trip so the developer can verify their data wrote correctly.
  3. Note Admin Portal enablement requirement at the top of the snippet if it's the first App Context code in the project.
  4. For membership operations, show the user object create + channel object create + membership create sequence (if Referential Integrity is on).
  5. Recommend filter expressions over client-side filtering for any list-and-filter pattern.
提供实现代码时:
  1. 当读取操作涉及
    custom
    字段时,务必包含
    include: { customFields: true }
  2. 展示设置+获取的完整流程,以便开发者验证数据是否正确写入。
  3. 如果是项目中首段App Context代码,请在代码片段顶部注明管理门户启用要求。
  4. 对于成员关系操作,展示用户对象创建+频道对象创建+成员关系创建的序列(当启用引用完整性时)。
  5. 对于任何列表+过滤模式,推荐使用过滤表达式而非客户端过滤。