@jamen/ws-extras v0.1.0
@jamen/ws-extras
Functions for using WebSockets on Node.js.
This package is inpsired after micro
, except made for WebSockets instead of HTTP. Its designed for use with ws
.
Usage
loop(conn, recv)
A loop for receiving messages from the connection through recv
. Once the connection closes the loop completes.
const WebSocket = require('ws')
const { loop } = require('@jamen/ws')
const server = new WebSocket.Server({ host, port })
server.on('connection', conn => loop(conn, recv))
async function recv (conn) {
// Receive messagess
}
To receive messages from conn
see the functions buffer
, text
, and json
.
buffer(conn, options)
text(conn, options)
json(conn, options)
Receive the next buffer, text, or JSON message.
The options
is an object that can contain
limit
: A byte amount of how large the message can be. Defaults to'1mb'
.timeout
: A duration to stop waiting for the message. Whenfalse
it never stops. Defaults to'1m'
.encoding
: The preferred encoding. Defaults to `'utf8'.
The function returns a promise resolving the result. The promise can reject with an error if the limit is reached, the timeout is reached, or an error from ws
emerges.
const { buffer, text, json } = require('@jamen/ws')
async function recv (conn) {
let message
// Next message as a buffer
message = await buffer(conn)
// As text
message = await text(conn, { limit: '5mb', timeout: '30s' })
// As JSON
message = await json(conn, { limit: 2097152, timeout: 60000 })
// Without timeout
message = await json(conn, { timeout: false })
}
jsonEvent(conn, options)
Similar to json
except only { event, data }
is allowed.
The options
are the same.
The returned promise can be rejected if the message is not a { event, data }
structure.
const { jsonEvent } = require('@jamen/ws')
async function recv (conn) {
const { name, data } = await jsonEvent(conn)
// ...
}
5 years ago