I need to indicate that you are incorrectly adding middleware to the application. A call to app.use should not be made in the app.get request app.get , but outside of it. Just call them directly after createServer or see other examples in the docs .
The secret you pass to express.session should be a string constant or possibly something taken from the configuration file. Do not feed him what the client may know, which is actually dangerous. This is a secret that only the server should know about.
If you want to keep the email address in the session, just do something line by line:
req.session.email = req.param('email');
With that aside ...
If I understand correctly, what you are trying to do is process one or more HTTP requests and monitor the session, and then open the Socket.IO connection from which you need the session data.
What difficulty in this problem lies in the fact that the Socket.IO tool for creating magic work on any http.Server is to capture the request event. Thus, Express' (or rather, Connect ), the session's middleware, is never called on a Socket.IO connection.
I believe that you can do this work, though, with some tricks.
You can get Connect session data; you just need to get a link to the session store. The easiest way to do this is to create a store before calling express.session :
Each session store has a get(sid, callback) method. The sid parameter or session identifier is stored in a cookie on the client. By default, connect.sid specified for this cookie. (But you can give it any name by specifying the key option in your express.session call.)
Then you need to access this cookie in the Socket.IO connection. Unfortunately, Socket.IO does not seem to give you access to http.ServerRequest . A simple job would be to get the cookie in the browser and send it through the Socket.IO connection.
The code on the server will look something like this:
var io = require('socket.io'), express = require('express'); var app = express.createServer(), socket = io.listen(app), store = new express.session.MemoryStore; app.use(express.cookieParser()); app.use(express.session({ secret: 'something', store: store })); app.get('/', function(req, res) { var old = req.session.email; req.session.email = req.param('email'); res.header('Content-Type', 'text/plain'); res.send("Email was '" + old + "', now is '" + req.session.email + "'."); }); socket.on('connection', function(client) {
It is assumed that you only want to read an existing session. You cannot create or delete sessions because Socket.IO connections may not have an HTTP response to send a Set-Cookie header (think WebSockets).
If you want to edit sessions, this may work with some session stores. For example, CookieStore does not work because it also needs to send the Set-Cookie header, which it cannot. But for other stores, you can try calling the set(sid, data, callback) method and see what happens.