@adonisjs/transmit-client

中文文档
1.1.0发布于 2026/01/18MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 6.4万

AdonisJS的本地服务器-Sent-Event模块的客户端.

中文使用文档
文档目录10 节
  1. 使用量 | 检查 AdonisJS
  2. 目录
  3. 安装
  4. 使用量
  5. 创建订阅
  6. 取消订阅
  7. 订阅请求
  8. 已认证的事件流
  9. 自定义 UID 生成器
  10. 重新连接

AdonisJS 传输客户端

AdonisJS的本地服务器-Sent-Event(SSE)模块的客户端.


typescript-image gh-workflow-image npm-image npm-download-image license-image



AdonisJS Transmit客户端是AdonisJS的本地服务器-Sent-Event(SSE)模块的客户端. 它建在顶上 事件来源 API并提供一个简单的API来接收来自服务器的事件.

目录

安装

从 npm 登记册中安装软件包如下:

npm i @adonisjs/transmit-client

使用量

模块显示 Transmit class,可以用来连接服务器并收听事件.

import { Transmit } from '@adonisjs/transmit-client'

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
})

创建订阅

这个 subscription 方法用于创建频道的订阅。 该方法接受频道名称

const subscription = transmit.subscription('chat/1')

那么,你必须打电话给 create 用于订阅后端注册的方法.

await subscription.create()

您可以使用 onMessage 方法。 您可以在同一订阅上定义尽可能多的听众.

subscription.onMessage((message) => {
  console.log(message)
})

您也可以只听一次使用 onMessagetOnce 方法.

subscription.onMessageOnce((message) => {
  console.log('I will be called only once')
})

Note 收听器只属于本地;您可以在服务器注册订阅之前或之后添加它们.

取消订阅

这个 onMessage 方法返回从订阅中删除信件处理器的函数.

const unsubscribe = subscription.onMessage(() => {
  console.log('message received!')
})

// later
unsubscribe()

如果您想从服务器上完全删除订阅, 您可以拨打 delete 方法.

await subscription.delete()

订阅请求

您可以使用 beforeSubscribe 或者说 beforeUnsubscribe 选项.

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  beforeSubscribe: (_request: Request) => {
    console.log('beforeSubscribe')
  },
  beforeUnsubscribe: (_request: Request) => {
    console.log('beforeUnsubscribe')
  },
})

已认证的事件流

这个 __transmit/events 使用 EventSource,它不能发送自定义标题。 这意味着 beforeSubscribe编号 :beforeUnsubscribe 只影响订阅/不订阅 HTTP 调用。 如果您依赖基于信头的认证, 请保护 __transmit/subscribe 财务报告和审定财务报表 __transmit/unsubscribe,或提供习惯 eventSourceFactory 可以发送信头.

使用示例 @microsoft/fetch-event-source数字 :

import { fetchEventSource } from '@microsoft/fetch-event-source'

function createFetchEventSource(
  url: string | URL,
  options: { withCredentials: boolean },
  headers: Record<string, string>
) {
  const controller = new AbortController()
  const listeners = new Map<string, Set<(event: MessageEvent) => void>>()

  const dispatch = (type: string, data?: string) => {
    const event = new MessageEvent(type, { data })
    listeners.get(type)?.forEach((listener) => listener(event))
  }

  fetchEventSource(url.toString(), {
    headers,
    credentials: options.withCredentials ? 'include' : 'omit',
    signal: controller.signal,
    onopen: () => dispatch('open'),
    onmessage: (message) => dispatch(message.event ?? 'message', message.data),
    onerror: () => {
      dispatch('error')
    },
  })

  return {
    addEventListener(type: string, listener: (event: MessageEvent) => void) {
      if (!listeners.has(type)) {
        listeners.set(type, new Set())
      }
      listeners.get(type)!.add(listener)
    },
    removeEventListener(type: string, listener: (event: MessageEvent) => void) {
      listeners.get(type)?.delete(listener)
    },
    close() {
      controller.abort()
    },
  } as EventSource
}

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  eventSourceFactory: (url, options) => {
    return createFetchEventSource(url, options, {
      Authorization: `Bearer ${token}`,
    })
  },
})

注意: 此适配器是最小的, 只有电线 open, (中文(简体) ), error,以及 message (或自定义事件名称). 如果你依赖别人 EventSource 功能类 readyState 或者说 onopen,你可能想要扩展它.

自定义 UID 生成器

默认情况下, 传输使用 crypto.randomUUID() 生成唯一的客户端标识符。 这种方法只在 安全环境 (HTTPS) (韩语). 如果您需要通过 HTTP 传输( 例如本地网络部署) , 您可以提供自定义 uidGenerator 函数.

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  uidGenerator: () => {
    return Array.from({ length: 16 }, () =>
      Math.floor(Math.random() * 256).toString(16).padStart(2, '0')
    ).join('')
  },
})

或者利用图书馆 uuid数字 :

import { v4 as uuid } from 'uuid'

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  uidGenerator: () => uuid(),
})

重新连接

传输客户端将在连接丢失时自动重接服务器. 可以改变重试和钩入重联生命周期的次数如下:

const transmit = new Transmit({
  baseUrl: 'http://localhost:3333',
  maxReconnectionAttempts: 5,
  onReconnectAttempt: (attempt) => {
    console.log('Reconnect attempt ' + attempt)
  },
  onReconnectFailed: () => {
    console.log('Reconnect failed')
  },
})

活动

这个 Transmit 类使用 EventTarget 类以释放多个事件.

transmit.on('connected', () => {
  console.log('connected')
})

transmit.on('disconnected', () => {
  console.log('disconnected')
})

transmit.on('reconnecting', () => {
  console.log('reconnecting')
})

这意味着您也可以通过通过事件听器本身的功能来去除先前注册的事件听器.

const onConnected = () => {
  console.log('connected')
}

transmit.on('connected', onConnected)
transmit.off('connected', onConnected)

版本与发布记录

@adonisjs/transmit-client 共发布 17 个版本,首次发布于 2023/07/08;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2026/01/189 个月前
1.0.02024/04/242 年前
0.2.22024/04/212 年前
0.2.12024/04/102 年前
0.2.02024/04/102 年前
0.1.62024/03/092 年前

关于 @adonisjs/transmit-client 的常见问题

@adonisjs/transmit-client 是什么?

AdonisJS的本地服务器-Sent-Event模块的客户端.

如何安装 @adonisjs/transmit-client?

在项目目录运行 npm install @adonisjs/transmit-client,或使用 pnpm add / yarn add / bun add。

@adonisjs/transmit-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adonisjs/transmit-client 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adonisjs/transmit-client 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.16.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@adonisjs/transmit-client 可以商用吗?

@adonisjs/transmit-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adonisjs/transmit-client 的下载量有多少?

根据 npm 官方下载统计,@adonisjs/transmit-client 在 2026/10/05 – 2026/11/03 这 30 天内被下载 63,919 次,日均约 2,131 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adonisjs/transmit-client 还在维护吗?

最新版本 1.1.0 发布于 2026/01/18(9 个月前);至今共发布 17 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/07/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。