Socket.io: Nodejs socket.io unhandled websocket unhandled error

Created on 31 Oct 2018  路  3Comments  路  Source: socketio/socket.io

I am using socket.io-stream library to transfer files between 2 node servers.

Server A: (Send File)

```
var clientIO = require('socket.io-client');
var ss = require('socket.io-stream');

var clientSocket= clientIO(http://ServerB_Address);
clientSocket.on('error', (error) => {
console.log(error);
});

var stream = ss.createStream({objectMode: true});
ss(clientSocket).emit('send-file', stream, data);
var uploadedBytes = 0;
var from = 0;
var reader = fs.createReadStream(filePath, {start: from});
reader.pipe(stream)
.on('error', (error) => {
console.log(error);
})
.on('data', (chunk) => {
uploadedBytes += Buffer.byteLength(chunk);
});

Server B: (Receive File)

 ```
var fs = require('fs');
    var io = require('socket.io')(http);
    var ss = require('socket.io-stream');

    io.on('connection', (socket) => {
        // listen to send file
        ss(socket).on('send-mail', (stream, data) => {
            console.log(`*** Begin receive file`);

            var writer = fs.createWriteStream(downloadPath, {flags: 'a'});
            stream.pipe(writer);

            var size = 0;
            stream.on('data', (chunk) => {
                size += chunk.length;
                console.log(`*** Receive ${size} from mail`);
            });

            stream.on('end', () => {
                console.log(`*** End file`);
            });

            stream.on('error', (error) => {
                console.log(`*** Error when receiving file`);
                console.log(error);
            });
        });
    });

It works fine, but when transferring file if the second node server is stopped, the app in the other server crashed and an unhandled error event was thrown

stream-error

Please can anyone tell me how can I catch this error? And if there is a better way to transfer files between node servers?

Most helpful comment

Don't use socket.io ..., you will save time in the long run, just look at the open 300+ open issues with no answers ...
Last significant commit was 6 month ago ...

All 3 comments

Use an event listener for the "error" event. Or use a pure event emitter library

How to use event listener for the error? Please give me an example, because I register error for client socket.

Don't use socket.io ..., you will save time in the long run, just look at the open 300+ open issues with no answers ...
Last significant commit was 6 month ago ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MyMomSaysIAmSpecial picture MyMomSaysIAmSpecial  路  4Comments

jloa picture jloa  路  4Comments

MichaelJCole picture MichaelJCole  路  3Comments

adammw picture adammw  路  4Comments

kootoopas picture kootoopas  路  4Comments