# @arcblock/ws

> OCAP Chain websocket server and client

Latest version **1.30.24** (published 2026-05-14) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @arcblock/ws
pnpm add @arcblock/ws
yarn add @arcblock/ws
bun add @arcblock/ws
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.30.24 |
| Published | 2026-05-14 |
| First published | 2021-09-29 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 59.3 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | arcblock |
| Maintainers | wangshijun, polunzh, mave99a, gxw |
| Keywords | websocket |

## Links

- npm: https://www.npmjs.com/package/@arcblock/ws
- Repository: https://github.com/ArcBlock/blockchain
- Homepage: https://github.com/ArcBlock/blockchain#readme
- Issues: https://github.com/ArcBlock/blockchain/issues
- npm.io page: https://npm.io/package/@arcblock/ws

## Dependencies (6)

- [ws](https://npm.io/package/ws.md) ^8.18.0
- [debug](https://npm.io/package/debug.md) ^4.4.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.23
- [phoenix](https://npm.io/package/phoenix.md) 1.7.2
- [eventemitter3](https://npm.io/package/eventemitter3.md) ^4.0.7
- [@arcblock/event-hub](https://npm.io/package/@arcblock/event-hub.md) 1.30.24

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.30.24 (latest) — 2026-05-14
- 1.30.23 — 2026-05-13
- 1.30.22 — 2026-05-13
- 1.30.21 — 2026-05-12
- 1.30.20 — 2026-05-12
- 1.30.19 — 2026-05-12
- 1.30.18 — 2026-05-11
- 1.30.17 — 2026-05-11
- 1.30.16 — 2026-05-10
- 1.30.15 — 2026-05-08
- 1.30.14 — 2026-05-08
- 1.30.13 — 2026-05-08
- 1.30.12 — 2026-05-05
- 1.30.11 — 2026-05-03
- 1.30.10 — 2026-04-22
- … 440 more at https://npm.io/package/@arcblock/ws/versions

## README

# Blocklet Server WebSocket

> Blocklet Server PubSub base on Websocket and Phoenix Protocol

## Usage

1. WsServer

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

2. WsClient

WsClient is inherited from [Phoenix](https://www.npmjs.com/package/phoenix)([source](https://github.com/phoenixframework/phoenix/blob/master/assets/js/phoenix.js)),

```javascript
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...
});
```

3. Hooks

It's very simple to create hooks in react apps.

---
_Source: https://npm.io/package/@arcblock/ws · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
