Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs socket.io unhandled websocket unhandled error #1246

Closed
karmac2015 opened this issue Oct 31, 2018 · 1 comment
Closed

Nodejs socket.io unhandled websocket unhandled error #1246

karmac2015 opened this issue Oct 31, 2018 · 1 comment

Comments

@karmac2015
Copy link

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?

@darrachequesne
Copy link
Member

Closed due to inactivity, please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants