socket.io middleware

http://socket.io/docs/server-api/#namespace#use(fn:function):namespace

Namespace#use(fn:Function):Namespace

Registers a middleware, which is a function that gets executed for
every incoming Socket and receives as parameter the socket and a
function to optionally defer execution to the next registered
middleware.

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

Errors passed to middleware callbacks are sent as special error
packets to clients.