As many comments have already been said, the Socket.IO API has changed in version 1.0 . Authentication should now be performed using the middleware function, see "Authentication Differences" @ http://socket.io/docs/migrating-from-0-9/#authentication-differences . I will include my original answer for those stuck at <1.0, as old documents seem to have disappeared.
1.0 and later:
Client side:
//The query member of the options object is passed to the server on connection and parsed as a CGI style Querystring. var socket = io("http://127.0.0.1:3000/", { query: "foo=bar" });
Server side:
io.use(function(socket, next){ console.log("Query: ", socket.handshake.query); // return the result of next() to accept the connection. if (socket.handshake.query.foo == "bar") { return next(); } // call next() with an Error if you need to reject the connection. next(new Error('Authentication error')); });
Pre 1.0
You can pass the request: param in the second argument to connect () on the client side, which will be available on the server in the authorization method.
I just tested it. On the client, I have:
var c = io.connect('http://127.0.0.1:3000/', { query: "foo=bar" });
On server:
io.set('authorization', function (handshakeData, cb) { console.log('Auth: ', handshakeData.query); cb(null, true); });
The result on the server looked like this:
:!node node_app/main.js info - socket.io started Auth: { foo: 'bar', t: '1355859917678' }
Alex Wright Dec 18 '12 at 19:48 2012-12-18 19:48
source share