@chanx-js/server

中文文档
2.2.0发布于 2026/11/03MITTypeScriptES Module构建来源已验证近 30 天下载 2110

chanx for Node: typed WebSocket consumers with topics and groups, speaking the same protocol as Python's chanx.

原始使用文档

@chanx-js/server

Type-safe WebSocket server for JavaScript and TypeScript. Declare messages once: each action gets a validated, typed handler, groups broadcast across processes, and the whole API is documented as AsyncAPI, from which @chanx-js/codegen generates a typed client.

Runs on Node with ws, Express, Koa, Fastify, Hono, NestJS, Bun and Deno.

Documentation · Getting started · Why chanx-js? · API

npm install @chanx-js/server ws zod
import { createServer } from 'node:http';
import { z } from 'zod';
import {
  PingMessage,
  PongMessage,
  consumer,
  createChanx,
  defineMessage,
} from '@chanx-js/server';
import { asyncapiMiddleware, attachChanx } from '@chanx-js/server/node';

const Chat = defineMessage('chat', z.object({ text: z.string() }));
const Notice = defineMessage('notice', z.object({ text: z.string() }));

const chat = consumer('/ws/chat/{room}', {
  name: 'chat',
  groups: (conn) => [`room.${conn.params.room}`],
})
  .on(PingMessage, { output: PongMessage }, () => PongMessage.create())
  .on(Chat, { output: Notice }, (m, ctx) =>
    ctx.broadcast(Notice.create({ text: m.payload.text })),
  );

const chanx = createChanx({ consumers: [chat] });
const server = createServer(asyncapiMiddleware(chanx)); // docs at /asyncapi
attachChanx(server, chanx);
server.listen(8000);
  • Handlers, not if/else: payloads are validated by any Standard Schema library (Zod, Valibot, ArkType) before your handler runs.
  • Groups and topics: broadcast to rooms, and let clients subscribe to many streams over one socket. Across processes with @chanx-js/layers.
  • Runs anywhere: Node's http, Express, Koa, Fastify, Hono, Bun, Deno, and NestJS. See integrations.
  • Testing: an in-process client and broadcast captures, from @chanx-js/server/testing.
  • Compatible with Python chanx: same protocol, same AsyncAPI, one Redis shared by both.

MIT licensed.

版本与发布记录

@chanx-js/server 共发布 11 个版本,首次发布于 2026/10/24;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.2.0latest2026/11/036 天前
2.1.42026/10/2713 天前
2.1.32026/10/2713 天前
2.1.22026/10/2614 天前
2.1.12026/10/2515 天前
2.1.02026/10/2515 天前

关于 @chanx-js/server 的常见问题

@chanx-js/server 是什么?

chanx for Node: typed WebSocket consumers with topics and groups, speaking the same protocol as Python's chanx.

如何安装 @chanx-js/server?

在项目目录运行 npm install @chanx-js/server,或使用 pnpm add / yarn add / bun add。

@chanx-js/server 支持 TypeScript 吗?

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

@chanx-js/server 支持 ESM 还是 CommonJS?

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

@chanx-js/server 需要什么版本的 Node.js?

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

@chanx-js/server 可以商用吗?

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

@chanx-js/server 的下载量有多少?

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

@chanx-js/server 还在维护吗?

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