{"id":19738268,"url":"https://github.com/httptoolkit/websocket-stream","last_synced_at":"2025-10-06T16:31:51.374Z","repository":{"id":57116672,"uuid":"410076910","full_name":"httptoolkit/websocket-stream","owner":"httptoolkit","description":"websockets with the node stream API","archived":false,"fork":false,"pushed_at":"2022-06-17T15:33:21.000Z","size":173,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-15T04:48:41.584Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"maxogden/websocket-stream","license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/httptoolkit.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-24T19:16:08.000Z","updated_at":"2023-05-09T22:10:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"447c3802-9f1f-4493-b34b-bc5212f92f79","html_url":"https://github.com/httptoolkit/websocket-stream","commit_stats":null,"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httptoolkit%2Fwebsocket-stream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httptoolkit%2Fwebsocket-stream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httptoolkit%2Fwebsocket-stream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/httptoolkit%2Fwebsocket-stream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/httptoolkit","download_url":"https://codeload.github.com/httptoolkit/websocket-stream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235534365,"owners_count":19005470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T01:13:42.995Z","updated_at":"2025-10-06T16:31:51.016Z","avatar_url":"https://github.com/httptoolkit.png","language":"JavaScript","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\u003e _Part of [HTTP Toolkit](https://httptoolkit.tech): powerful tools for building, testing \u0026 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","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhttptoolkit%2Fwebsocket-stream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhttptoolkit%2Fwebsocket-stream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhttptoolkit%2Fwebsocket-stream/lists"}