---
url: /reference/interface/bridge/realtime.md
---

# realtime

`realtime` 允许你的应用之间可以实时通信。

导入：

```typescript
import { realtime } from "@pc-nexus/bridge";
```

内置方法：

## 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;
}
```

### 参数

`RealtimePublishOptions` 类型定义如下：

`scope` 类型定义如下：

### 返回值

返回值类型为 `RealtimePublishResult` :

### 示例

```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;
}
```

### 参数

`RealtimeSubscribeOptions` 类型定义同 `publish` 一致。

### 返回值

返回一个 `Promise<Subscription>` 订阅对象，可进行取消订阅：

```typescript
interface Subscription {
  unsubscribe: () => void;
}
```

`Subscription` 类型定义如下：

### 示例

```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();
```
