max-mapper / websocket-stream

websockets with the node stream API
BSD 2-Clause "Simplified" License
668 stars 114 forks source link

question - use case - need help #110

Closed ORESoftware closed 7 years ago

ORESoftware commented 7 years ago

hey @maxogden!

my question is whether this module might fit my use case - I can't tell.

What I want to do is send data from the browser to a server, and I want to be able the stream that data to a writable stream on a Node.js server. That seems to be what this module is all about.

I don't see a need to load this library's code in the browser - I just have a standard websocket client in the browser and then on the server just use this library's code to create a WSS that can read streams.

mcollina commented 7 years ago

that's exactly fine. You can also use it in the browser, if you want to, but that's up to you. It works with straight websockets.

ORESoftware commented 7 years ago

Ok, but if you use a websocket client in the browser how do you signal that the stream is ended or closed? If I am not mistaken the readme could use that that info.

ORESoftware commented 7 years ago

For example, on the server we have this:

let websocket = require('websocket-stream')
let wss = websocket.createServer({server: someHTTPServer}, function handle(stream) {
  fs.createReadStream('bigdata.json').pipe(stream)
});

on the client we have:

let socket = require('socket.io-client')('http://localhost');
socket.on('connect', function(){
   socket.emit('first chunk');
   socket.emit('second chunk');
   socket.emit('third chunk');
   socket.end();   // this doesn't really exist
});

so as you can see, I think it's unclear how to close/end/finish the stream coming from the client.

Am I right? Should this be added to the readme?

Do we need to close the socket connection in the client in order for the stream to be closed? What call do we need to use?

mcollina commented 7 years ago

@ORESoftware this does not work with socket.io. For socket.io, just use socket.io server counterparts. You can use https://developer.mozilla.org/it/docs/WebSockets on the browser.

ORESoftware commented 7 years ago

@mcollina thanks, if I use https://developer.mozilla.org/it/docs/WebSockets, how do I signal that the read stream has ended? That is my main question.

mcollina commented 7 years ago

from the server side or the client side? you just call end() on the server side (or just wait for the piped source to finish) or you call close() on the client side if the data is flowing from the client to the server.

ORESoftware commented 7 years ago

ok thanks, calling close() on the client side is the information I am looking for - I think this should be mentioned in the readme, don't you think?

mcollina commented 7 years ago

This is the standard Websocket interface. Where would you see this documented? Would you like to send a PR?

ORESoftware commented 7 years ago

Yeah I will send a PR once I get it working with my own implementation