nkzawa / socket.io-stream

Stream for Socket.IO
MIT License
612 stars 111 forks source link

Stream.pipe event 'end' triggers only once #96

Open dmarman opened 7 years ago

dmarman commented 7 years ago

I have a server and a client set up with nodejs. The server is sending a file to the client with socket.io-stream. When the file is transfered I want to start working with it on the client side. For that I am listening to the 'end' event of the stream. My code works but only once. On the first file I send the event is triggered, but not on the next ones. After the first file, the next ones are corrupted and are 0 Bytes, I guess that's why the event is never triggered again.

server.js

 socketObj.emit('order', orderData);

 ss(socketObj).on('file', function(stream) {
            fs.createReadStream('./uploads/'+ orderData.file.name +'').pipe(stream);
 });

client.js

socket.on('order', function(data) {

    var stream = ss.createStream();

    ss(socket).emit('file', stream);

    stream.pipe(fs.createWriteStream(data.file.name));

    stream.on('end', function(){
        doSomeStuff(data); //This function is called only once
    });

});

I've tried using 'end' and 'finish' events, setting autoClose as true and false, using stream.close() and stream.end(), but nothing works. For me is like something is not resetting properly.

steppe87 commented 4 years ago

I run into the same issue. How did you reset your connection properly?

dmarman commented 4 years ago

I couldn't solve it. Instead of using websockets for the stream, I just sent a string message via websocket to the client saying that the file was ready to download. After that the client makes a get request and downloads the file via https.