Sails.js + Passport.js authentication through websockets

Alternatively, you can hijack the ‘router:request’ event to plug in passport for socket requests. I do this in ‘config/bootstrap.js’: module.exports.bootstrap = function (cb) { var passport = require(‘passport’), initialize = passport.initialize(), session = passport.session(), http = require(‘http’), methods = [‘login’, ‘logIn’, ‘logout’, ‘logOut’, ‘isAuthenticated’, ‘isUnauthenticated’]; sails.removeAllListeners(‘router:request’); sails.on(‘router:request’, function(req, res) { initialize(req, res, function () { … Read more

socket.io private message

No tutorial needed. The Socket.IO FAQ is pretty straightforward on this one: socket.emit(‘news’, { hello: ‘world’ }); EDIT: Folks are linking to this question when asking about how to get that socket object later. There is no need to. When a new client connects, save a reference to that socket on whatever object you’re keeping … Read more

socket.io send packet to sender only

When your server listens, you usually get a socket at the “connection” event : require(‘socket.io’).on(‘connect’, function(socket){ A socket connects 2 points : the client and the server. When you emit on this socket, you emit to this specific client. Example : var io = require(‘socket.io’); io.on(‘connect’, function(socket){ socket.on(‘A’, function(something){ // we just received a message … Read more

socket.io / parameters on connection

Yes, there is. 1) query is like GET parameters, so replace “param:value” with “param=value” (if you want to pass multiple parameters, do it as you usually do with URL: param=value&some_other_param=test) 2) There is an easier and more reliable (because there is no risk to access an undefined property of handshaken object) way to get query … Read more