bare-ws
Reference for bare-ws: a WebSocket client and server for Bare, built on bare-tcp/bare-tls via bare-http1/bare-https.
bare-ws is a WebSocket client and server for Bare. Sockets are bare-stream duplex streams that perform the WebSocket handshake over an HTTP or HTTPS request. It's pure JavaScript.
npm i bare-wsUsage
const ws = require('bare-ws')
const server = new ws.Server({ port: 8080 }, (socket) => {
socket.on('data', (data) => {
console.log(data.toString())
})
})
server.on('listening', () => {
const socket = new ws.Socket({ port: 8080 })
socket.write('Hello WebSocket')
})API
Socket
const socket = new ws.Socket(url[, options])
Open a WebSocket connection to url, or wrap an already-connected socket when acting as the server side of a handshake.
Overloads:
new ws.Socket(url, options)
new ws.Socket(options)options = {
host: null,
hostname: null, // Alias for `host`, for Node.js compatibility
path: null,
port: null,
secure: false, // Use TLS (`wss:`) for the underlying connection
socket: null // An already-connected TCP socket to wrap instead of opening a new one
}socket.ping(data) · socket.pong(data)
Send a ping or pong frame with data as its payload. A string data is converted to a Buffer. Throws a WebSocketError with code NOT_CONNECTED if the socket hasn't finished connecting.
ws.Socket.handshake(req, cb)
Perform the client side of the WebSocket opening handshake over the HTTP request req, calling cb(error) with a WebSocketError if the server's response is invalid.
Sockets emit ping and pong (in addition to the usual stream events).
Server
const server = new ws.Server([options][, onconnection])
Create a WebSocket server, optionally backed by options.server. onconnection is added as a connection listener, called for each successful WebSocket upgrade with (socket, req).
options = {
secure: false // Create an HTTPS-backed server, accepting `wss:` connections
}server.address() · server.listening
server.address() returns the bound address of the underlying TCP server. server.listening is true once the server is bound and accepting connections.
server.close([cb]) · server.ref() · server.unref()
Stop the server from accepting new connections, calling cb once closed; ref/unref the underlying server.
ws.Server.handshake(req, [socket[, head]], cb)
Perform the server side of the WebSocket opening handshake for the request req, writing the 101 upgrade response and calling cb(error) with a WebSocketError on failure.
Servers emit connection and listening.
Constants
ws.opcode holds the WebSocket frame opcodes (CONTINUATION, TEXT, BINARY, CLOSE, PING, PONG) defined by RFC 6455. ws.status holds close status codes for protocol errors (PROTOCOL_ERROR) and oversized messages (MESSAGE_TOO_LARGE).
Errors
Protocol violations throw a WebSocketError carrying a code and a close status:
| Code | Thrown when |
|---|---|
EXPECTED_CONTINUATION | A fragmented message's next frame wasn't a continuation frame. |
EXPECTED_MASK | A frame from a client was missing its required mask. |
INCOMPLETE_FRAME | The buffered data doesn't yet contain a full frame. |
INVALID_ACCEPT_HEADER | The server's Sec-WebSocket-Accept response header didn't match the expected digest. |
INVALID_ENCODING | Data was written with an encoding other than buffer or utf8. |
INVALID_KEY_HEADER | The Sec-WebSocket-Key header was missing or malformed. |
INVALID_OPCODE | A frame was received with an opcode that isn't TEXT or BINARY. |
INVALID_PAYLOAD_LENGTH | A frame's payload length field was invalid. |
INVALID_UPGRADE_HEADER | The Upgrade header was missing or not websocket. |
INVALID_VERSION_HEADER | The Sec-WebSocket-Version header was neither 8 nor 13. |
NETWORK_ERROR | The underlying HTTP request errored before the handshake completed. |
NOT_CONNECTED | An operation such as ping() or pong() was attempted before the socket finished connecting. |
UNEXPECTED_CONTINUATION | A continuation frame was received without a preceding fragmented frame. |
UNEXPECTED_CONTROL | A control frame was received while a fragmented message was in progress. |
UNEXPECTED_RSV1 / UNEXPECTED_RSV2 / UNEXPECTED_RSV3 | A frame was received with a reserved RSV bit set. |
Related modules
Builds on bare-crypto, bare-events, bare-http1, bare-https, and bare-stream (see Bare modules).
See also
- Bare modules—the full
bare-*catalog. bare-tcp—the socket layer WebSocket connections run over.bare-fetch—a request/response HTTP client for the non-persistent case.