realtime
本文档介绍实时消息推送 APIs,适用于将实时更新发送到当前页面之外或多个用户的场景。
导入:
typescript
import { realtime } from "@pc-nexus/realtime";内置方法:
| API | 描述 |
|---|---|
publish | 进行实时消息发布 |
signToken | 生成自定义令牌,对实时消息的权限范围进行更精细的控制 |
publish
publish 将数据实时推送到通道中。
函数签名
typescript
function publish<TPayload>(
channel: string,
payload: TPayload,
options?: RealtimePublishOptions
): Promise<RealtimePublishResult>;
export type RealtimePublishOptions =
| RealtimePublishSelfOptions
| RealtimePublishGlobalOptions
| RealtimePublishTokenOptions
| RealtimePublishContextOptions;
export interface RealtimePublishSelfOptions {
scope: "self";
}
export interface RealtimePublishGlobalOptions {
scope: "global";
}
export interface RealtimePublishTokenOptions {
scope: "token";
token: string;
}
export interface RealtimePublishContextOptions {
scope: "context";
context: RealtimeContext;
}
type RealtimeScope = "self" | "global" | "token" | "context";
type RealtimeContext =
| "project"
| "workitem"
| "sprint"
| "product"
| "idea"
| "ticket"
| "space"
| "page"
| "library"
| "testcase"
| "testplan"
| "baseline"
| "release"
| "plan"
| "widget"
| "dashboard"
| "entry";
interface RealtimePublishResult {
message_id: string;
}参数
| 名称 | 描述 |
|---|---|
channel | 通道唯一标识名称 |
payload | 发布的数据内容,可以是字符串或可序列化的对象 |
options | 发布行为的配置项 |
RealtimePublishOptions 类型定义如下:
| 名称 | 描述 |
|---|---|
scope | 定义消息广播的作用范围 |
token | 使用鉴权 Token 来指定授权可接收消息的目标群体,仅在 scope = 'token' 时传入 |
context | 指定消息绑定的业务实体上下文,仅在 scope = 'context' 时传入 |
返回值
返回值类型为 RealtimePublishResult :
| 名称 | 描述 |
|---|---|
message_id | 事件发布成功后,返回的消息唯一标识 |
示例
typescript
import { realtime } from '@pc-nexus/realtime';
const result = await realtime.publish("workitem-updated", { workitemId: "xxx", field: "title", changelog: { origin: "标题1", target: "标题2" } });
const result = await realtime.publish("project-updated", { message: "xxx" }, {
scope: 'global'
})
const result = await realtime.publish("testcase-updated", { message: "from testcase-1" }, {
scope: 'token',
token: 'xxxx' // 通过 signToken 签发的 token
})
const result = await realtime.publish("workitem-update", { message: "update workitem title" }, {
scope: 'context',
context: 'workitem'
})signToken
给 channel 加一层由你控制的访问约束。订阅方 token 里的 claims ,要和发布方 token 里的 claims 匹配,前端才会收到消息。
函数签名
typescript
function signToken(
channel: string,
claims: Record<string, unknown>,
permissions?: RealtimeTokenPermission | RealtimeTokenPermission[]
): Promise<SignTokenResult>;
type RealtimeTokenPermission = 'subscribe' | 'publish';
interface SignTokenResult {
token: string;
expires_at: number;
}参数
| 名称 | 描述 |
|---|---|
channel | 通道唯一标识名称 |
claims | 签发 token 需要加密的数据对象。 |
permissions | 签发 token 时要确定的操作权限,默认: ['subscribe', 'publish'] |
返回值
返回值类型为 SignTokenResult :
| 名称 | 描述 |
|---|---|
token | 签发的 token 值。 |
expires_at | token 的过期时间。 |
示例
typescript
import { realtime } from '@pc-nexus/realtime';
const result = await realtime.signToken("workitem-updated", { workitemId: "xxx" });
const result = await realtime.signToken("workitem-updated", { projectId: "xxx" }, "subscribe");
const result = await realtime.signToken("workitem-updated", { projectId: "xxx" }, ["subscribe"]);错误处理
发生错误时响应都会附带一个包含更多信息的错误代码,下面列出了所有可能的错误代码、它们的含义以及可以采取哪些措施来解决。
| 错误码 | 描述 |
|---|---|
ERR_REALTIME_SIGN_TOKEN_INVALID | 签发 Token 请求的参数无效。 |
ERR_REALTIME_PUBLISH_INVALID | 发布通知请求的参数无效。 |
ERR_REALTIME_SUBSCRIBE_INVALID | 订阅通知请求的参数无效。 |
ERR_REALTIME_TOKEN_INVALID | 订阅或发布通知的 Token 验证失败。 |
ERR_REALTIME_REQUEST_RATE_LIMIT | 请求触发频率限制。 |
ERR_REALTIME_EVENTS_RATE_LIMIT | 发布通知触发频率限制。 |