node.js - How to tie up express sessions with socket.io -
how tie sessions in express 4 socket.io session data can both retrieved , saved in both directions?
authentication in socket.io token-based jwt
the same socket shared in multiple vhosts
eg:
global.socket.of('/secure').on('connection', function(socket){ console.log('client connected "secure" socket'); });
app.js
var port = 3000, jwtsecret = '26fed98d8c6c7c54f485f1ee7ffe44d2e1feae28'; var app = require('express')(), vhost = require('vhost'), server = require('http').createserver(app), socketiojwt = require('socketio-jwt'); // start server socket.io global.socket = require('socket.io').listen(app.listen(port, function(){ console.log('express server listening on port '+port); })); // socket authentication global.socket.set('authorization', socketiojwt.authorize({ secret: jwtsecret, handshake: true })); // virtual hosts app.use(vhost('secure.domain.com', require('./vhost/secure')(jwtsecret)));
vhost/secure.js
var express = require('express'), http = require('http'), jwt = require('jsonwebtoken'); function host(jwtsecret){ var app = express(); // session app.use(require('cookie-parser')()); app.use(require('express-session')({ secret: '0a6e0caf5477cee686726ad06284309eb4a580e4', key: 'express.sid', cookie: { maxage : 1000*60*10 } })); // http 200 app.get('/', function(req, res){ res.end('helle world!'); }); app.post('/login', function(req, res){ var profile = { id: 1, name: 'username' }; var token = jwt.sign(profile, jwtsecret, {expiresinminutes: 60}); res.json({token: token}); }); global.socket.of('/secure').on('connection', function(socket){ console.log('client connected "secure" socket'); socket.on('disconnect', function(){ console.log('client disconnected "secure" socket'); }); }); return http.createserver(app); } module.exports = host;
Comments
Post a Comment