Authenticate socket.io incoming connections with JWTs. https://www.npmjs.com/package/@thream/socketio-jwt
This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
2013-01-07 11:30:15 -03:00
lib fix bug calling both success and accept 2013-01-07 11:30:15 -03:00
test fix bug calling both success and accept 2013-01-07 11:30:15 -03:00
.gitignore initial 2012-09-05 15:14:36 -03:00
package.json refactorize to use xtend 2013-01-07 10:40:52 -03:00
README.md add basic integration tests 2012-11-16 12:43:12 -03:00

Access Passport.js user information from socket.io connection.

Installation

npm install passport.socketio

Usage


  //configure passport and express

  var socketIo = require("socket.io"),
    passportSocketIo = require("passport.socketio");

  var sio = socketIo.listen(webServer);

  sio.set("authorization", passportSocketIo.authorize({
    sessionKey:    'express.sid',      //the cookie where express (or connect) stores its session id.
    sessionStore:  mySessionStore,     //the session store that express uses
    sessionSecret: "my session secret", //the session secret to parse the cookie
    fail: function(data, accept) {     // *optional* callbacks on success or fail
      accept(null, false);             // second param takes boolean on whether or not to allow handshake
    },
    success: function(data, accept) {
      accept(null, true);
    }
  }));

  sio.sockets.on("connection", function(socket){
    console.log("user connected: ", socket.handshake.user.name);
    
    //filter sockets by user...
    var userGender = socket.handshake.user.gender, 
        opposite = userGender === "male" ? "female" : "male";

    passportSocketIo.filterSocketsByUser(sio, function (user) {
      return user.gender === opposite;
    }).forEach(function(s){
      s.send("a " + userGender + " has arrived!");
    });

  });

Develop

npm install npm test

License

MIT - José F. Romaniello 2012.