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.
socketio-jwt/README.md

68 lines
1.7 KiB
Markdown
Raw Normal View History

2012-09-05 20:15:53 +02:00
Access [Passport.js](http://passportjs.org) user information from [socket.io](http://socket.io) connection.
2012-09-05 20:14:36 +02:00
Installation
============
```
npm install passport.socketio
```
Usage
=====
```javascript
//configure passport and express
var socketIo = require("socket.io"),
passportSocketIo = require("passport.socketio");
var sio = socketIo.listen(webServer);
//except for the optional fail and success the parameter object has the
//same attribute than the session middleware http://www.senchalabs.org/connect/middleware-session.html
2012-09-05 20:14:36 +02:00
sio.set("authorization", passportSocketIo.authorize({
key: 'express.sid', //the cookie where express (or connect) stores its session id.
secret: 'my session secret', //the session secret to parse the cookie
store: mySessionStore, //the session store that express uses
2012-10-26 18:19:01 +02:00
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);
}
2012-09-05 20:14:36 +02:00
}));
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!");
});
});
```
2012-11-16 16:43:12 +01:00
Develop
=======
npm install
npm test
2012-09-05 20:14:36 +02:00
License
========
MIT - José F. Romaniello 2012.