Modular, extensible and operations-friendly WebSocket framework for
Node.js. This release focuses on a small, stable core so dependent projects can proceed; extensions (channels, auth, history, events, admin, routing, heartbeat, message-meta,
permessage-deflate) exist as planned work and will be completed and documented in follow-up
releases.
Status: core stable and testable โ extensions work in progress (APIs present in repository; integration examples available in examples/).
createRegistry() โ lightweight connection registry
with broadcast and auto-clean.attachServer(server, opts) โ simple HTTP upgrade
wiring.permessage-deflate (RFC 7692)
provided as an optional extension instance.Extensions (channels, events, history,
admin, routing,
message-meta, heartbeat) are available as separate modules
in the repo but marked WIP โ README below points to that.
npm:
npm install ws13
yarn:
yarn add ws13
This example uses the core createWebSocket exported from the package.
const http = require('http');
const createWebSocket = require('ws13');
const server = http.createServer();
const { registry } = createWebSocket.attachServer(server, {
onConnect(ws, req) {
ws.send('Welcome');
ws.on('message', (evt) => {
// simple echo
ws.send(`Echo: ${evt.data}`);
});
}
});
server.listen(8080);
Client (Node.js reusing createWebSocket in client mode):
const http = require('http');
const createWebSocket = require('ws13');
const req = http.request({ host: '127.0.0.1', port: 8080, path: '/' });
const ws = createWebSocket({ request: req });
ws.on('open', () => ws.send('hello'));
ws.on('message', (evt) => console.log(evt.data));
Browser clients should use the native WebSocket API (wss:// for TLS).
createWebSocket(options): create client or server
WebSocket-like instancerequest (IncomingMessage |
ClientRequest), socket, protocol, origin,
heartbeatInterval_ms, extension (or null), autoReconnect,
requestFactory, shouldReconnect, isDebug.send(data), sendPing(data), sendPong(data),
heartbeat(cb), close(code, reason). Properties: readyState,
ip, port, latency_ms, bufferedAmount,
protocol, extensions, url.createRegistry(): returns
{ add(ws), delete(ws), broadcast(data), size(), clients:Set }registry auto-cleans clients on
close/error.attachServer(server, { registry?, onConnect? }):
attaches upgrade handler and returns { registry }.permessage-deflate extension is provided
in core (createPermessageDeflate) and can be disabled via createWebSocket({ extension: null }).
TypeScript definitions are available at 'index.d.ts' for full shapes and options.
Project ships basic tests for the core. Use test-runner-lite (or Node directly) to run tests.
Run all tests:
npm test
Run the core test directly:
node ./index.test.js
Example core tests included:
Add more tests as needed; tests live next to core and in each extension folder when implemented.
The following extensions exist as separate modules and will be fully documented and stabilised in subsequent releases. Current status: prototype/partial implementations present in repo.
channels โ channel-based pub/submessage-meta โ typed messages with meta
(wrap/unwrap)heartbeat โ idle/timeout hooks and monitor
history โ per-channel replay buffer (last N
messages)events โ JSON-RPC style event emitter
(ws.onEvent / ws.emitEvent)admin โ HTTP + WebSocket admin API, CSV/JSON
export, disconnect/latency endpoints, live dashboardrouting โ targeted delivery (sendToUser,
sendToRole, sendToIp)auth โ authentication and role-based
authorizationIf your project relies on a specific extension, tell me which one and I will prioritise finishing it and publishing a stable interface.
heartbeatInterval_ms in core is 30s (can
be changed per socket).maxDecompressSize to protect against decompression attacks.ws13/
core/
index.d.ts
index.js
index.test.js
permessage-deflate.js
README.md
extensions/
admin/
index.d.ts
index.js
index.test.js
README.md
express-middleware.d.ts
express-middleware.js
examples/
admin-api/
server.js
admin-dashboard/
admin-dashboard.html
admin-express-integration/
index.js
README.md
auth/
index.d.ts
index.js
index.test.js
README.md
examples/
auth-channels-client.js
auth-channels-server.js
server-with-admin.js
channels/
index.d.ts
index.js
index.test.js
README.md
examples/
channels/
client.js
server.js
events/
index.d.ts
index.js
index.test.js
README.md
examples/
events/
client.js
server.js
heartbeat/
index.d.ts
index.js
index.test.js
README.md
examples/
heartbeat/
client.js
history/
index.d.ts
index.js
index.test.js
README.md
sqlite-adapter.js
sqlite-adapter.test.js
examples/
history/
client.js
server.js
message-meta/
index.d.ts
index.js
index.test.js
README.md
examples/
message-meta/
client.js
server.js
routing-server.js
routing/
index.d.ts
index.js
index.test.js
README.md
examples/
routing/
client.js
server.js
logo/
logo-ws13.png
browser.js
index.d.ts
index.js
LICENSE
package.json
README.md
testRunner.js
wrapper.mjs
channels, history, events,
auth, admin or
routing) and Iโll prepare stable API docs,
tests, and examples.This project is licensed under the MIT License.
Copyright ยฉ Manuel Lรตhmus