WebSocket Stream Adapter
websocket-stream provides an interface to WebSockets using the Node.js Streams API, allowing developers to pipe data directly to and from a WebSocket connection. It is designed to work seamlessly in both Node.js environments and modern browsers that support WebSockets, making it a versatile tool for real-time applications. The current stable version is 5.5.2, with minor and patch releases occurring periodically, indicating active maintenance. Key differentiators include its adherence to the Node.js Streams API for managing WebSocket data, offering a familiar and composable interface compared to directly manipulating `WebSocket` instances. It integrates well with server-side WebSocket libraries like `ws` and can be used with frameworks such as Express via `express-ws`, streamlining the creation of full-duplex communication channels.
Common errors
-
TypeError: websocket.createServer is not a function
cause Attempting to import `createServer` as a named export from `websocket-stream` in an ESM context, or incorrectly destructuring it from the default import.fixThe `createServer` method is a property of the default export. Use `import websocket from 'websocket-stream'; websocket.createServer(...)`. -
Error: WebSocket is not open: readyState 0 (CONNECTING)
cause Attempting to write to the WebSocket stream before the connection is fully established. The stream is in `CONNECTING` state (readyState 0) and not yet `OPEN` (readyState 1).fixEnsure you wait for the WebSocket connection to be open before piping data. You can listen for the `open` event on `ws.socket` or the `ready` event on the stream itself if provided by an abstraction layer, or simply ensure your stream pipeline starts after connection is established. -
ReferenceError: require is not defined in ES module scope
cause Using `require('websocket-stream')` in a TypeScript or JavaScript file configured for ESM, or directly in a browser environment without a bundler.fixFor ESM projects, use `import websocket from 'websocket-stream';`. If targeting browsers, ensure you are using a bundler like Browserify or Webpack that handles CommonJS modules for client-side code.
Warnings
- breaking Major version updates (v4.0.0, v5.0.0) updated the underlying `ws` dependency, which might introduce breaking changes from `ws` itself, such as API changes or different behaviors in message handling and options.
- gotcha The `perMessageDeflate` option default value differs between client (true) and server (false), and this option is ignored by browser clients. This can lead to unexpected compression behavior or performance issues if not explicitly configured consistently.
- gotcha Options for `websocket-stream` vary between browser and Node.js environments. Options like `browserBufferSize` and `browserBufferTimeout` are specific to browser clients and have no effect in Node.js.
- deprecated Older versions might have used deprecated `Buffer` constructors, which can lead to runtime warnings or security issues in newer Node.js environments.
Install
-
npm install websocket-stream -
yarn add websocket-stream -
pnpm add websocket-stream
Imports
- websocket
const websocket = require('websocket-stream')import websocket from 'websocket-stream'
- createServer
import { createServer } from 'websocket-stream'import websocket from 'websocket-stream'; websocket.createServer(...)
- websocketStream
import { websocketStream } from 'websocket-stream'import websocketStream from 'websocket-stream/stream'
Quickstart
import websocket from 'websocket-stream';
import { createReadStream, createWriteStream } from 'fs';
const ws = websocket('ws://echo.websocket.org');
// Pipe stdin to the WebSocket
process.stdin.pipe(ws);
// Pipe WebSocket data to stdout
ws.pipe(process.stdout);
// Example of piping a file to the WebSocket
// const fileStream = createReadStream('data.txt');
// fileStream.pipe(ws);
// Example of receiving data from WebSocket and saving to a file
// const outputFileStream = createWriteStream('received.txt');
// ws.pipe(outputFileStream);
console.log('Connected to ws://echo.websocket.org. Type something and press Enter!');