Skip to content

Instantly share code, notes, and snippets.

@lpinca
Created June 16, 2019 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpinca/114a348f6108acc1e4236fed05522f73 to your computer and use it in GitHub Desktop.
Save lpinca/114a348f6108acc1e4236fed05522f73 to your computer and use it in GitHub Desktop.
websocket-stream does not handle backpressure
'use strict';
const WebSocket = require('ws');
const websocketStream = require('websocket-stream');
const { Readable, Writable } = require('stream');
const chunk = Buffer.alloc(1024);
const src = new Readable({
read() {
this.push(chunk);
}
});
const dest = new Writable({
write(chunk, encoding, callback) {
setTimeout(callback, 10);
}
});
const server = new WebSocket.Server({ port: 0 }, function() {
const { port } = this.address();
const ws = websocketStream(`ws://localhost:${port}`);
ws.pipe(dest);
});
server.on('connection', function(ws) {
const dest = new Writable({
write(chunk, encoding, callback) {
ws.send(chunk, callback);
}
});
src.pipe(dest);
});
setInterval(function() {
console.log(process.memoryUsage());
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment