socket.io-session-middleware
Share connect/express sessions with socket.io 1.x
Last updated 5 years ago by peerigon .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install socket.io-session-middleware 
SYNC missed versions from official npm registry.

socket.io-session-middleware

Share connect/express sessions with socket.io 1.x

Setup

npm status

Dependency Status

Example

Server

var socketSession = require("socket.io-session-middleware");
    
var session = {
    store: new connect.session.MemoryStore(),
    secret: "secret",
    key: "mykey.sid",
    cookieParser: connect.cookieParser("secret")
};

io.use(socketSession(session));

io.on("connection", function(socket){

    socket.on("whoAreYou", function(callback){
        
        //read from session
        callback(socket.session.name);
    });
    
    socket.on("setName", function(data) {
        
        //write to session
        socket.session.name = data.name;
    })
});

Client


socket.emit("setName", { name: "hans" });

socket.emit("whoAreYou", function(name) { 
    console.log("I am " + name); 
});


//=> I am hans 

A full featured example can be found in the example folder.

Notes

Make sure to fire a http request to initialize the session/cookie before accessing the session with socket.io. If you are serving the socket.io client with your node.js server this won't be a problem for you.

Current Tags

  • 0.1.0                                ...           latest (5 years ago)

5 Versions

  • 0.1.0                                ...           5 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |