Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send an error packet to a single client in socket io?

I want to be able to emit an error packet to a client, similarly to how it's used when registering middleware.
This works:

var io = require('socket.io')();
io.use(function(socket, next){
  if (socket.request.headers.cookie) return next();
  next(new Error('Authentication error'));
});

This doesn't:

io.on('connection', function(socket){
    socket.emit('error', {errorMessage: 'errorMessage'});
    //or
    socket.emit('error', new Error('error'));
});

Error: Uncaught, unspecified "error" event.

How do I emit an error event using the socket object?

like image 666
LifeQuery Avatar asked Aug 17 '15 09:08

LifeQuery


1 Answers

Event name 'error' is reserved for transport level errors. When you will try

io.on('connection', function(socket){
    socket.emit('exception', {errorMessage: 'errorMessage'});
});

it should work just fine.

like image 189
galethil Avatar answered Sep 28 '22 19:09

galethil