Pipe Node.js streams over WebSockets (with back-pressure!).
npm install ws-streamify
Utility to pipe Node.js streams over WebSockets in a native way.
net.Socket in your browser :)!Yo Dawg I heard you like TCP, so we put a TCP in yo TCP so you can stream while u stream
$ npm install ws-streamify
`
It has support for Node.js >= 0.12. Almost zero dependencies (for browsers, however, it requires polyfills for some Node.js core libraries which is not a problem with webpack or browserify).Usage
`javascript
// socket is a W3C compliant WebSocket object
// options is a common options object for Node.js streams
new WebSocketStream(socket, options)
``javascript
// server.jsimport WebSocket from 'ws'
import WebSocketStream from 'ws-streamify'
WebSocket.Server({ port: 8000 }).on('connection', (ws) => {
let stream = new WebSocketStream(ws, { highWaterMark: 1024 })
fs.createReadStream(path.join(__dirname, 'lorem.txt')).pipe(stream)
})
``javascript
// client.jsimport WebSocketStream from 'ws-streamify'
let socket = new WebSocket('ws://localhost:8000')
let stream = new WebSocketStream(socket, { highWaterMark: 1024 })
`Running tests
`
$ npm install
$ npm test
`Running example
`
$ npm install
$ npm start
`
It will start a server on localhost with a simple example page. You can click on it and it will read text from the stream and print it. In the terminal and browser consoles flow control messages will appear. Also, check Network -> ws://localhost:8000/ -> Frames` in the Chrome DevTools.