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 #3363

Closed
karmac2015 opened this issue Oct 31, 2018 · 4 comments
Closed

Nodejs socket.io unhandled websocket unhandled error #3363

karmac2015 opened this issue Oct 31, 2018 · 4 comments

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?

@GrosSacASac
Copy link

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

@karmac2015
Copy link
Author

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

@GrosSacASac
Copy link

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 ...

@darrachequesne
Copy link
Member

The socket.io-stream project seems unmaintained (last commit was 5 years ago), but if there is enough requests I could contact the owner and take over the project.

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

3 participants