{"_id":"@httptoolkit/websocket-stream","_rev":"4096384","name":"@httptoolkit/websocket-stream","description":"Use websockets with the node streams API. Works in browser and node, with all current WS versions","dist-tags":{"latest":"6.0.1"},"maintainers":[{"name":"pimterry","email":"pimterry@gmail.com"}],"time":{"modified":"2025-12-08T22:52:34.000Z","created":"2021-09-30T08:05:43.840Z","6.0.1":"2022-06-17T15:36:25.300Z","6.0.0":"2021-09-30T08:05:43.840Z"},"users":{},"author":"","repository":{"type":"git","url":"git+ssh://git@github.com/httptoolkit/websocket-stream.git"},"versions":{"6.0.1":{"name":"@httptoolkit/websocket-stream","version":"6.0.1","license":"BSD-2-Clause","description":"Use websockets with the node streams API. Works in browser and node, with all current WS versions","scripts":{"test":"node test.js","start":"beefy test-client.js"},"repository":{"type":"git","url":"git+ssh://git@github.com/httptoolkit/websocket-stream.git"},"keywords":["websocket","websockets","stream","streams","realtime"],"dependencies":{"@types/ws":"*","duplexify":"^3.5.1","inherits":"^2.0.1","isomorphic-ws":"^4.0.1","readable-stream":"^2.3.3","safe-buffer":"^5.1.2","ws":"*","xtend":"^4.0.0"},"devDependencies":{"@types/node":"^11.13.4","beefy":"^2.1.8","browserify":"^16.2.3","concat-stream":"^1.6.2","tape":"^4.9.1","typescript":"^3.4.3"},"optionalDependencies":{},"browser":{"./echo-server.js":"./fake-server.js","./index.js":"./stream.js","ws":"./ws-fallback.js"},"bugs":{"url":"https://github.com/httptoolkit/websocket-stream/issues"},"homepage":"https://github.com/httptoolkit/websocket-stream#readme","main":"index.js","author":"","types":"./index.d.ts","gitHead":"9f15121fe33a82aba829f2e9275c82f815d4b201","_id":"@httptoolkit/websocket-stream@6.0.1","_nodeVersion":"14.18.3","_npmVersion":"8.5.2","dist":{"shasum":"8d732f1509860236276f6b0759db4cc9859bbb62","size":8823,"noattachment":false,"key":"/@httptoolkit/websocket-stream/-/@httptoolkit/websocket-stream-6.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@httptoolkit/websocket-stream/download/@httptoolkit/websocket-stream-6.0.1.tgz"},"_npmUser":{"name":"pimterry","email":"pimterry@gmail.com"},"directories":{},"maintainers":[{"name":"pimterry","email":"pimterry@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/websocket-stream_6.0.1_1655480185106_0.37515002057582536"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-06-17T16:01:10.870Z","publish_time":1655480185300,"_cnpm_publish_time":1655480185300},"6.0.0":{"name":"@httptoolkit/websocket-stream","version":"6.0.0","license":"BSD-2-Clause","description":"Use websockets with the node streams API. Works in browser and node, with all current WS versions","scripts":{"test":"node test.js","start":"beefy test-client.js"},"repository":{"type":"git","url":"git+ssh://git@github.com/httptoolkit/websocket-stream.git"},"keywords":["websocket","websockets","stream","streams","realtime"],"dependencies":{"@types/ws":"*","duplexify":"^3.5.1","inherits":"^2.0.1","isomorphic-ws":"^4.0.1","readable-stream":"^2.3.3","safe-buffer":"^5.1.2","ws":"*","xtend":"^4.0.0"},"devDependencies":{"@types/node":"^11.13.4","beefy":"^2.1.8","browserify":"^16.2.3","concat-stream":"^1.6.2","tape":"^4.9.1","typescript":"^3.4.3"},"optionalDependencies":{},"browser":{"./echo-server.js":"./fake-server.js","./index.js":"./stream.js","ws":"./ws-fallback.js"},"bugs":{"url":"https://github.com/httptoolkit/websocket-stream/issues"},"homepage":"https://github.com/httptoolkit/websocket-stream#readme","main":"index.js","author":"","gitHead":"0522d75a0a45837e3b068c804aa69ed246d4816d","_id":"@httptoolkit/websocket-stream@6.0.0","_nodeVersion":"14.17.0","_npmVersion":"6.14.13","dist":{"shasum":"72d00c06944057951d7ab8a8f8336033c7b52e90","size":8886,"noattachment":false,"key":"/@httptoolkit/websocket-stream/-/@httptoolkit/websocket-stream-6.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@httptoolkit/websocket-stream/download/@httptoolkit/websocket-stream-6.0.0.tgz"},"_npmUser":{"name":"pimterry","email":"pimterry@gmail.com"},"directories":{},"maintainers":[{"name":"pimterry","email":"pimterry@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/websocket-stream_6.0.0_1632989143686_0.40790165803307254"},"_hasShrinkwrap":false,"publish_time":1632989143840,"_cnpm_publish_time":1632989143840,"_cnpmcore_publish_time":"2021-12-16T19:18:46.939Z"}},"readme":"# websocket-stream [![Build Status](https://github.com/httptoolkit/websocket-stream/workflows/CI/badge.svg)](https://github.com/httptoolkit/websocket-stream/actions) [![Get it on npm](https://img.shields.io/npm/v/@httptoolkit/websocket-stream.svg)](https://www.npmjs.com/package/@httptoolkit/websocket-stream)\n\n> _Part of [HTTP Toolkit](https://httptoolkit.tech): powerful tools for building, testing & debugging HTTP(S)_\n\nUse HTML5 [websockets](https://developer.mozilla.org/en-US/docs/WebSockets) using the Node Streams API.\n\nThis is a fork of the original [websocket-stream](https://www.npmjs.com/package/websocket-stream) (now unmaintained), for use in HTTP Toolkit. This fork:\n\n* Uses [isomorphic-ws](https://www.npmjs.com/package/isomorphic-ws) so that the 'ws' module is not loaded unnecessarily in browsers\n* Supports WS up to version 8 (note that isomorphic-ws uses * as its ws version, so this will always use the latest compatible release)\n* Fixes and extends the TypeScript types, and adds an explicit dependency on @types/ws\n* Has a CI build for automated testing\n* Adds a 'ws-close' event, which exposes the close event data when a websocket is closed by a close frame (before the stream 'close' event, which is different)\n\n### Usage\n\nThis module works in Node or in Browsers that support WebSockets. You can use [browserify](http://github.com/substack/node-browserify) to package this module for browser use.\n\n```javascript\nvar websocket = require('@httptoolkit/websocket-stream')\nvar ws = websocket('ws://echo.websocket.org')\nprocess.stdin.pipe(ws)\nws.pipe(process.stdout)\n```\n\nIn the example above `ws` is a duplex stream. That means you can pipe output to anything that accepts streams. You can also pipe data into streams (such as a webcam feed or audio data).\n\nThe underlying `WebSocket` instance is available as `ws.socket`.\n\n#### Options\n\nThe available options differs depending on if you use this module in the browser or with node.js. Options can be passed in as the third or second argument - `WebSocket(address, [protocols], [options])`.\n\n##### `options.browserBufferSize`\n\nHow much to allow the [socket.bufferedAmount](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Attributes) to grow before starting to throttle writes. This option has no effect in node.js.\n\nDefault: `1024 * 512` (512KiB)\n\n##### `options.browserBufferTimeout`\n\nHow long to wait before checking if the socket buffer has drained sufficently for another write. This option has no effect in node.js.\n\nDefault: `1000` (1 second)\n\n##### `options.objectMode`\n\nSend each chunk on its own, and do not try to pack them in a single\nwebsocket frame.\n\nDefault: `false`\n\n##### `options.binary`\n\nAlways convert to `Buffer` in Node.js before sending.\nForces `options.objectMode` to `false`.\n\nDefault: `true`\n\n##### `options.perMessageDeflate`\n\nWe recommend disabling the [per message deflate\nextension](https://tools.ietf.org/html/rfc7692) to achieve the best\nthroughput.\n\nDefault: `true` on the client, `false` on the server.\n\nExample:\n\n```js\nvar websocket = require('@httptoolkit/websocket-stream')\nvar ws = websocket('ws://realtimecats.com', {\n  perMessageDeflate: false\n})\n```\n\nBeware that this option is ignored by browser clients. To make sure that permessage-deflate is never used, disable it on the server.\n\n##### Other options\n\nWhen used in node.js see the [ws.WebSocket documentation](https://github.com/websockets/ws/blob/master/doc/ws.md#class-wswebsocket)\n\n### On the server\n\nUsing the [`ws`](http://npmjs.org/ws) module you can make a websocket server and use this module to get websocket streams on the server:\n\n```javascript\nvar websocket = require('@httptoolkit/websocket-stream')\nvar wss = websocket.createServer({server: someHTTPServer}, handle)\n\nfunction handle(stream, request) {\n  // `request` is the upgrade request sent by the client.\n  fs.createReadStream('bigdata.json').pipe(stream)\n}\n```\n\nWe recommend disabling the [per message deflate\nextension](https://tools.ietf.org/html/rfc7692) to achieve the best\nthroughput:\n\n```javascript\nvar websocket = require('@httptoolkit/websocket-stream')\nvar wss = websocket.createServer({\n  perMessageDeflate: false,\n  server: someHTTPServer\n}, handle)\n\nfunction handle(stream) {\n  fs.createReadStream('bigdata.json').pipe(stream)\n}\n```\n\nYou can even use it on express.js with the [express-ws](https://www.npmjs.com/package/express-ws) library:\n\n```js\nconst express = require('express');\nconst expressWebSocket = require('express-ws');\nconst websocketStream = require('websocket-stream/stream');\nconst app = express();\n\n// extend express app with app.ws()\nexpressWebSocket(app, null, {\n    // ws options here\n    perMessageDeflate: false,\n});\n\napp.ws('/bigdata.json', function(ws, req) {\n  // convert ws instance to stream\n  const stream = websocketStream(ws, {\n    // websocket-stream options here\n    binary: true,\n  });\n\n  fs.createReadStream('bigdata.json').pipe(stream);\n});\n\napp.listen(3000);\n```\n\n## Run the tests\n\n### Server-side tests\n\n```\nnpm test\n```\n\n### Client-side tests\n\nFirst start the echo server by running `node test-server.js`\n\nThen run `npm start` and open `localhost:9966` in your browser and open the Dev Tools console to see test output.\n\n## license\n\nBSD LICENSE\n","_attachments":{},"homepage":"https://github.com/httptoolkit/websocket-stream#readme","bugs":{"url":"https://github.com/httptoolkit/websocket-stream/issues"},"license":"BSD-2-Clause"}