Gathering detailed insights and metrics for websocket-stream
Gathering detailed insights and metrics for websocket-stream
Gathering detailed insights and metrics for websocket-stream
Gathering detailed insights and metrics for websocket-stream
websockets with the node stream API
npm install websocket-stream
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
668 Stars
224 Commits
113 Forks
19 Watching
2 Branches
30 Contributors
Updated on 29 Sept 2024
Minified
Minified + Gzipped
JavaScript (91.18%)
TypeScript (8.82%)
Cumulative downloads
Total Downloads
Last day
9.2%
62,519
Compared to previous day
Last week
10.1%
331,260
Compared to previous week
Last month
6.3%
1,299,804
Compared to previous month
Last year
23.2%
17,046,763
Compared to previous year
6
7
Use HTML5 websockets using the Node Streams API.
This module works in Node or in Browsers that support WebSockets. You can use browserify to package this module for browser use.
1var websocket = require('websocket-stream') 2var ws = websocket('ws://echo.websocket.org') 3process.stdin.pipe(ws) 4ws.pipe(process.stdout)
In 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).
The underlying WebSocket
instance is available as ws.socket
.
The 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])
.
options.browserBufferSize
How much to allow the socket.bufferedAmount to grow before starting to throttle writes. This option has no effect in node.js.
Default: 1024 * 512
(512KiB)
options.browserBufferTimeout
How long to wait before checking if the socket buffer has drained sufficently for another write. This option has no effect in node.js.
Default: 1000
(1 second)
options.objectMode
Send each chunk on its own, and do not try to pack them in a single websocket frame.
Default: false
options.binary
Always convert to Buffer
in Node.js before sending.
Forces options.objectMode
to false
.
Default: true
options.perMessageDeflate
We recommend disabling the per message deflate extension to achieve the best throughput.
Default: true
on the client, false
on the server.
Example:
1var websocket = require('websocket-stream') 2var ws = websocket('ws://realtimecats.com', { 3 perMessageDeflate: false 4})
Beware that this option is ignored by browser clients. To make sure that permessage-deflate is never used, disable it on the server.
When used in node.js see the ws.WebSocket documentation
Using the ws
module you can make a websocket server and use this module to get websocket streams on the server:
1var websocket = require('websocket-stream') 2var wss = websocket.createServer({server: someHTTPServer}, handle) 3 4function handle(stream, request) { 5 // `request` is the upgrade request sent by the client. 6 fs.createReadStream('bigdata.json').pipe(stream) 7}
We recommend disabling the per message deflate extension to achieve the best throughput:
1var websocket = require('websocket-stream') 2var wss = websocket.createServer({ 3 perMessageDeflate: false, 4 server: someHTTPServer 5}, handle) 6 7function handle(stream) { 8 fs.createReadStream('bigdata.json').pipe(stream) 9}
You can even use it on express.js with the express-ws library:
1const express = require('express'); 2const expressWebSocket = require('express-ws'); 3const websocketStream = require('websocket-stream/stream'); 4const app = express(); 5 6// extend express app with app.ws() 7expressWebSocket(app, null, { 8 // ws options here 9 perMessageDeflate: false, 10}); 11 12app.ws('/bigdata.json', function(ws, req) { 13 // convert ws instance to stream 14 const stream = websocketStream(ws, { 15 // websocket-stream options here 16 binary: true, 17 }); 18 19 fs.createReadStream('bigdata.json').pipe(stream); 20}); 21 22app.listen(3000);
npm test
First start the echo server by running node test-server.js
Then run npm start
and open localhost:9966
in your browser and open the Dev Tools console to see test output.
BSD LICENSE
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 7/20 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More