pubnub-app-context
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePubNub 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,channelevent types)membership
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
核心工作流程
- Enable App Context add-on in the Admin Portal for the keyset.
- Choose a bucket region (cannot change later without migration).
- Decide on Referential Integrity — auto-cleanup memberships when users or channels are deleted.
- Define your schema for fields on users, channels, and memberships.
custom - Implement set/get operations with whenever you need custom data.
includeCustomFields: true - Subscribe to metadata events if your UI needs to react to profile or membership changes.
- Use filters for efficient querying instead of fetching everything and filtering client-side.
- 在管理门户中为密钥集启用App Context插件。
- 选择存储桶区域(后续无法更改,除非进行迁移)。
- 确定引用完整性设置——当用户或频道被删除时,是否自动清理其成员关系。
- 定义用户、频道和成员关系的字段 schema。
custom - 实现设置/获取操作,当需要自定义数据时,务必添加。
includeCustomFields: true - 订阅元数据事件(如果您的UI需要对资料或成员关系变更做出响应)。
- 使用过滤器进行高效查询,而非获取全部数据后在客户端进行过滤。
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:
- App Context add-on enabled
- Bucket Region selected (data residency choice — cannot change without migration)
- User/Channel/Membership Metadata Events configured if your clients need to react to changes
- 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的密钥集必须满足:
- 启用App Context插件
- 选择存储桶区域(数据驻留选项——除非迁移,否则无法更改)
- 配置用户/频道/成员关系元数据事件(如果您的客户端需要对变更做出响应)
- 确定引用完整性策略:当用户或频道被删除时,是否自动级联清理其成员关系?
有关密钥集的常规配置,请查看 pubnub-keyset-management/references/keysets-and-environments.md。
Three Object Types
三种对象类型
| Object | API surface | Identifier | Holds |
|---|---|---|---|
| User (UUID Metadata) | | | name, email, externalId, profileUrl, custom fields |
| Channel (Channel Metadata) | | channel name | name, description, custom fields |
| Membership (User-in-Channel) | | (userId, channelId) pair | custom fields per relationship |
| 对象 | API接口 | 标识符 | 存储内容 |
|---|---|---|---|
| 用户(UUID元数据) | | | 姓名、邮箱、externalId、profileUrl、自定义字段 |
| 频道(频道元数据) | | 频道名称 | 名称、描述、自定义字段 |
| 成员关系(用户-频道关联) | | (userId, channelId) 配对 | 每个关联关系的自定义字段 |
Custom Fields Are First-Class
自定义字段为一等公民
The field on each object holds your application-specific data. Use scalar values (strings, numbers, booleans). Nested objects are supported but keep them shallow.
customjavascript
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'
}
}
});每个对象的字段用于存储您的应用专属数据。使用标量值(字符串、数字、布尔值)。支持嵌套对象,但请保持层级较浅。
customjavascript
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读取自定义数据必须设置includeCustomFields: true
includeCustomFields: trueThe 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 , the object is omitted from the response.
customFields: truecustomApp Context最常见的错误:在读取操作中忘记启用自定义字段选项。
javascript
const result = await pubnub.objects.getUUIDMetadata({
uuid: 'user-123',
include: { customFields: true }
});
console.log(result.data.custom);如果未设置,响应中将省略对象。
customFields: truecustomConstraints
限制条件
- 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 when reading if your code touches
include.customFields: true.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:
- — create, read, update, delete users, channels, and memberships from the Admin Portal API
manage_app_context
当此技能激活时,优先使用:
- — 通过管理门户API创建、读取、更新、删除用户、频道和成员关系
manage_app_context
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 initialization and
new PubNub(...)requirements (the sameuserIdis the App Context user identifier)userId - 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即为App Context的用户标识符)userId - 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:
- Always include on read operations that touch
include: { customFields: true }.custom - Show set + get round-trip so the developer can verify their data wrote correctly.
- Note Admin Portal enablement requirement at the top of the snippet if it's the first App Context code in the project.
- For membership operations, show the user object create + channel object create + membership create sequence (if Referential Integrity is on).
- Recommend filter expressions over client-side filtering for any list-and-filter pattern.
提供实现代码时:
- 当读取操作涉及字段时,务必包含
custom。include: { customFields: true } - 展示设置+获取的完整流程,以便开发者验证数据是否正确写入。
- 如果是项目中首段App Context代码,请在代码片段顶部注明管理门户启用要求。
- 对于成员关系操作,展示用户对象创建+频道对象创建+成员关系创建的序列(当启用引用完整性时)。
- 对于任何列表+过滤模式,推荐使用过滤表达式而非客户端过滤。