@stone-js/node-ws-adapter
v0.8.15
Published
Node.js WebSocket adapter for Stone.js: runs a `ws` server and bridges sockets to @stone-js/realtime (channels, presence, gateways), with optional per-message kernel dispatch.
Downloads
1,766
Maintainers
Readme
Stone.js - Node WebSocket Adapter
Node.js WebSocket adapter for Stone.js. Runs a ws server and bridges every socket to @stone-js/realtime: each connection is added to the shared connection store (so broadcast reaches it) and its lifecycle (connect, subscribe, unsubscribe, message, error, disconnect) is dispatched into the realtime router, firing your @On* gateways. Data frames can optionally run through the Stone.js kernel too. The core is never touched: this adapter is pure Integration.
Installation
npm install @stone-js/node-ws-adapter
# the WebSocket server (optional peer, imported lazily):
npm install wsPeer dependencies:
@stone-js/coreand@stone-js/realtime.wsis an optional peer, imported lazily when the server starts.
Enable it
import { StoneApp } from '@stone-js/core'
import { Realtime } from '@stone-js/realtime'
import { NodeWs } from '@stone-js/node-ws-adapter'
@NodeWs({ url: 'ws://localhost:8080' })
@Realtime({ driver: 'memory' })
@StoneApp({ name: 'app' })
export class Application {}That is all: connect a client, subscribe to a channel, and broadcast from anywhere reaches it.
import { RealtimeGateway, OnConnect, OnEvent, connectionOf } from '@stone-js/realtime'
@RealtimeGateway()
export class Chat {
constructor (private readonly realtime) {}
@OnConnect() onConnect (_, event) { const connection = connectionOf(event) /* … */ }
@OnEvent('room:1', 'message')
async onMessage (payload, event) {
await this.realtime.to('room:1').emit('message', payload)
}
}How it works
- The adapter runs a
wsserver bound tostone.adapter.url(defaultws://localhost:8080). - Each accepted socket becomes a realtime
Connectionwith asend, added to the shared connection store. - Control frames (
{ type: 'subscribe' | 'unsubscribe', channel }) update presence. - Every socket event is normalized into an
IncomingEvent, keyed by its lifecycle orevent:channel:eventkey, and run through the kernel, where the light key-router routes it to the matching@On*gateway. If a gateway returns content, it is sent back to the sender.
Configuration
| key | default | notes |
| ------------------------------- | -------------------- | ------------------------------------------------- |
| stone.adapter.url | ws://localhost:8080| The bind URL (host + port). |
| stone.adapter.server | {} | Options passed to the ws WebSocketServer. |
| stone.adapter.serverFactory | undefined | Inject a custom/fake server (tests, attach to an http server). |
