@arcblock/ws

中文文档
1.30.26发布于 2026/11/05Apache-2.0TypeScriptESM + CJS近 30 天下载 6173

会计师办 链式 Websocket 服务器和客户端

中文使用文档

块服务器 WebSocket

Blocklet 服务器 PubSub 基址在 Websocket 和 Phoenix 协议

使用量

  1. 服务器
const { WsServer } = require('@arcblock/ws');

/**
 * @params {Object} opts
 *  @params {http.Server} opts.httpServer
 *  @params {String} opts.pathname default to '/websocket'
 *  @params {Function} opts.authenticate
 */
const wsServer = new WsServer({
  httpServer: http.createServer(),
  authenticate: (req, cb) => {
    const { searchParams } = new URL(req.url, `http://${req.headers.host || 'unknown'}`);
    const token = searchParams.get('token');
    if (!token) {
      cb(new Error('token not found'), null);
      return;
    }

    // custom logic for validate token
    const authInfo = validateToken(token);

    // if validate success
    cb(null, authInfo);

    // if validate error
    cb(new Error('validate fail'), null);
  },
});

// attach to httpServer(httpServer has passed by constructor)
wsSerer.attach();

// push message
wsServer.broadcast('blocklet.installed', data);
wsServer.broadcast('notification.create', data);
wsServer.broadcast('topic', 'event', data);
  1. 宽度

Wsclient 继承自 凤凰(单位:千美元)来源, (中文(简体) ),

import WsClient from '@arcblock/ws/lib/client';

// create instance
const socket = new WsClient(`//${window.location.hostname}`, {
  // params will be passed to server through url
  params: () => ({
    // token is used for authentication
    token: window.localStorage.getItem('abt_node_login_token'),
  }),

  // Defaults to none
  logger: (type, msg, data) => console.log(type, msg, data),
});

// connect
socket.connect();

// add subscriber
socket.on('blocklet.installed', callback1);
socket.on('notification.create', callback2);

// remove subscriber
socket.off('blocklet.installed', callback1);
socket.off('notification.create', callback2);

// More flexible subscription
const subscription = socket.subscribe('topic', {});
subscription.on('event', ({ response }) => {
  // Do something with the event data
});

// disconnect
socket.disconnect(() => {
  // after disconnected...
});
  1. 钩子

在反应应用中创建钩子非常简单.

版本与发布记录

@arcblock/ws 共发布 457 个版本,首次发布于 2021/09/29;过去 12 个月发布了 77 个版本。

版本发布日期距今
1.30.26latest2026/11/052 天前
1.30.252026/11/043 天前
1.30.242026/05/145 个月前
1.30.232026/05/135 个月前
1.30.222026/05/135 个月前
1.30.212026/05/135 个月前

关于 @arcblock/ws 的常见问题

@arcblock/ws 是什么?

会计师办 链式 Websocket 服务器和客户端

如何安装 @arcblock/ws?

在项目目录运行 npm install @arcblock/ws,或使用 pnpm add / yarn add / bun add。

@arcblock/ws 支持 TypeScript 吗?

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

@arcblock/ws 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@arcblock/ws 可以商用吗?

@arcblock/ws 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@arcblock/ws 的下载量有多少?

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

@arcblock/ws 还在维护吗?

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