---
url: /reference/functions/realtime/realtime.md
---

# realtime

本文档介绍实时消息推送 APIs，适用于将实时更新发送到当前页面之外或多个用户的场景。

导入：

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

内置方法：

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

### 参数

`RealtimePublishOptions` 类型定义如下：

### 返回值

返回值类型为 `RealtimePublishResult` :

### 示例

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

### 参数

### 返回值

返回值类型为 `SignTokenResult` :

### 示例

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