├── .gitignore ├── views └── index.ejs ├── README.md ├── package.json └── app.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |This is answer for question from stackoverflow.
8 | 9 | 10 | 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### socket.io and express session solution 2 | 3 | This example show how to use [socket.io](https://github.com/Automattic/socket.io) with [express.js 4](https://github.com/strongloop/express) and handle session. It's answer for stackoverflow [question](http://stackoverflow.com/questions/24679046/expressjssocket-ioexpress-session). 4 | 5 | To run example just simply run 6 | 7 | ``` bash 8 | $ npm install 9 | $ npm start 10 | ``` 11 | 12 | and navigate to [http://127.0.0.1:8888/](http://127.0.0.1:8888/). -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "socket.io-express-solution", 3 | "version": "0.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "Artur Delura", 11 | "license": "MIT", 12 | "dependencies": { 13 | "socket.io": "^2.0.4", 14 | "express": "~4.16.2", 15 | "express-session": "~1.15.6", 16 | "cookie": "*", 17 | "cookie-parser": "*", 18 | "ejs": "2.5.7" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var app = require('express')(), 2 | session = require('express-session'), 3 | cookie = require('cookie'), 4 | cookieParser = require('cookie-parser'), 5 | sessionStore = new session.MemoryStore(); 6 | 7 | var COOKIE_SECRET = 'secret'; 8 | var COOKIE_NAME = 'sid'; 9 | 10 | app.set('views', __dirname + '/views'); 11 | app.set('view engine', 'ejs'); 12 | app.use(cookieParser(COOKIE_SECRET)); 13 | app.use(session({ 14 | name: COOKIE_NAME, 15 | store: sessionStore, 16 | secret: COOKIE_SECRET, 17 | saveUninitialized: true, 18 | resave: true, 19 | cookie: { 20 | path: '/', 21 | httpOnly: true, 22 | secure: false, 23 | maxAge: null 24 | } 25 | })); 26 | 27 | // HTTP session cookie is set here 28 | // Must appear after session middleware 29 | app.get('/', function (req, res) { 30 | res.render('index'); 31 | }); 32 | 33 | var server = require('http').Server(app).listen(8888), 34 | io = require('socket.io')(server); 35 | 36 | io.use(function(socket, next) { 37 | try { 38 | var data = socket.handshake || socket.request; 39 | if (! data.headers.cookie) { 40 | return next(new Error('Missing cookie headers')); 41 | } 42 | console.log('cookie header ( %s )', JSON.stringify(data.headers.cookie)); 43 | var cookies = cookie.parse(data.headers.cookie); 44 | console.log('cookies parsed ( %s )', JSON.stringify(cookies)); 45 | if (! cookies[COOKIE_NAME]) { 46 | return next(new Error('Missing cookie ' + COOKIE_NAME)); 47 | } 48 | var sid = cookieParser.signedCookie(cookies[COOKIE_NAME], COOKIE_SECRET); 49 | if (! sid) { 50 | return next(new Error('Cookie signature is not valid')); 51 | } 52 | console.log('session ID ( %s )', sid); 53 | data.sid = sid; 54 | sessionStore.get(sid, function(err, session) { 55 | if (err) return next(err); 56 | if (! session) return next(new Error('session not found')); 57 | data.session = session; 58 | next(); 59 | }); 60 | } catch (err) { 61 | console.error(err.stack); 62 | next(new Error('Internal server error')); 63 | } 64 | }); 65 | --------------------------------------------------------------------------------