realtime
realtime 允许你的应用之间可以实时通信。
导入:
typescript
import { realtime } from "@pc-nexus/bridge";内置方法:
| API | 描述 |
|---|---|
subscribe | 订阅当前扩展应用上下文的实时通道 |
publish | 向指定的一个通道推送事件或消息 |
publish
publish 将数据实时推送到通道中。
函数签名
typescript
function publish<TPayload>(
channel: string,
payload: TPayload,
options?: RealtimePublishOptions
): Promise<RealtimePublishResult>;
interface RealtimePublishOptions {
scope?: RealtimeScope;
token?: string;
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 | 通道唯一标识名称,必须与 subscribe 侧严格一致 |
payload | 发布的数据内容 |
options | 发布行为的配置项 |
RealtimePublishOptions 类型定义如下:
| 名称 | 描述 |
|---|---|
scope | 定义消息广播的作用范围,发布与订阅的 scope 必须相同;使用 token 或 context 时,对应参数也必须一致。不传时默认为 self ,且仅限当前安装的应用。 |
token | 使用鉴权 Token 来指定授权可接收消息的目标群体,仅在 scope = 'token' 时传入 |
context | 指定消息绑定的业务实体上下文,仅在 scope = 'context' 时传入 |
scope 类型定义如下:
| 名称 | 描述 |
|---|---|
self | 当前所在的扩展。消息只在这个扩展内互通。 |
global | 同一个应用安装下的所有扩展。这些扩展之间可以互通。 |
context | 同一个业务对象下的所有扩展可以互通。需要同时传入 context 。 例如:传 context = 'project' 表示同一项目,传 context = 'workitem' 表示同一工作项,其他取值同理。 |
token | 持有相同 claims 的已签发令牌的连接。令牌中的声明一致才会互相收到,用来自己指定接收人群。需要同时传入 token 。 |
返回值
返回值类型为 RealtimePublishResult :
| 名称 | 描述 |
|---|---|
message_id | 事件发布成功后,返回的消息唯一标识 |
示例
typescript
import { realtime } from '@pc-nexus/bridge';
realtime.publish("workitem-updated", { message: "from workitem-1" });
realtime.publish("project-updated", { message: "from project-1" }, {
scope: 'global'
})
realtime.publish("testcase-updated", { message: "from testcase-1" }, {
scope: 'token',
token: 'xxxx' // 通过 signToken 签发的 token
})
realtime.publish("workitem-update", { message: "update workitem title" }, {
scope: 'context',
context: 'workitem'
})subscribe
subscribe 订阅通道用于实时通信。
函数签名
typescript
function subscribe<TPayload>(
channel: string,
callback: (payload?: TPayload>) => void,
options?: RealtimeSubscribeOptions
): Promise<Subscription>;
interface RealtimeSubscribeOptions {
scope?: RealtimeScope;
token?: string;
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 Subscription {
unsubscribe: () => void;
}参数
| 名称 | 描述 |
|---|---|
channel | 通道唯一标识名称,必须与 publish 侧严格一致 |
callback | 收到事件时的回调函数 |
options | 订阅行为的配置项 |
RealtimeSubscribeOptions 类型定义同 publish 一致。
返回值
返回一个 Promise<Subscription> 订阅对象,可进行取消订阅:
typescript
interface Subscription {
unsubscribe: () => void;
}Subscription 类型定义如下:
| 名称 | 描述 |
|---|---|
unsubscribe | 取消订阅函数 |
示例
typescript
import { realtime } from '@pc-nexus/bridge';
const subscription1 = await realtime.subscribe("workitem-updated", (payload) => {
console.log("收到消息:", payload);
});
// 取消订阅
subscription1.unsubscribe();
const subscription2 = await realtime.subscribe("project-updated", (payload) => {
console.log("收到消息:", payload);
}, {
scope: 'global'
})
subscription2.unsubscribe();
const subscription3 = await realtime.subscribe("testcase-updated", (payload) => {
console.log("收到消息:", payload);
}, {
scope: 'token',
token: 'xxxx' // 通过 signToken 签发的 token
})
subscription3.unsubscribe();
const subscription4 = await realtime.subscribe("workitem-update", (payload) => {
console.log("收到消息:", payload);
}, {
scope: 'context',
context: 'workitem'
})
subscription4.unsubscribe();