Skip to content

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_attoken 的过期时间。

示例 ​

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发布通知触发频率限制。

举报邮箱:abuse@worktile.com