0.1.8 • Published 2 years ago

rttl v0.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago