rttl
v1.0.1
Published
Realtime transport layer
Downloads
37
Readme
Rttl
Realtime transport layer. Supports websocket (ws) and uWebsocket.js.
Quick start
Installation
npm install --save rttl
Create websocket server over http(s) or uWebSockets
import * as http from "http"
import { WebSocketTransport } from "rttl"
const server = new http.Server()
const rtt = new WebSocketTransport({ server })
uWebSockets.js supported
import uWebSockets from "uWebsockets.js"
import { uWebSocketTransport } from "rttl"
const server = uWebSockets.App()
const rtt = new uWebSocketTransport({ server })
Add transport event handlers
// handle client connection event
rtt.onConnection((client: Client) => {
// ...
})
// handle incoming messages
rtt.onMessage((client: Client, data: Buffer, isBinary: boolean) => {
const message = isBinary ? data : JSON.parse(data.toString())
// ...
})
// handle client disconnection
rtt.onDisconnect((client: Client, code: number, data: string) => {
// ...
})
// handle errors
rtt.onError((error: Error) => {
// ...
})
Gracefull shutdown
rtt.close()
Inject mock client
// inject mock socket
const ws = wst.inject(`/test?param=1`, { headers: { "test-header": "1234" } })
// handle message events
ws.onmessage = (event) => {
const message = JSON.parse(event.data)
// ...
}
// send message from mock socket
ws.send(JSON.stringify({ type: "text", text: "test"}))
License
MIT