Created
November 14, 2012 03:38
-
-
Save bmatusiak/4070108 to your computer and use it in GitHub Desktop.
express/mongodb socket.io/redis AIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Session Storage for multi servers ex.Heroku stacks | |
* for expressjs socket.io with databases redis and mongodb | |
* (redis/socket.io) | |
* (mongodb/expressjs) | |
* | |
* Bradley Matusiak @ 2012 bmatusiak@gmail.com | |
* | |
* ref:bunch of searches from google | |
**/ | |
var rtg = require("url").parse(REDIS_URI); | |
var mgl = require("url").parse(MONGO_URI); | |
//------- | |
/* | |
npm install express socket.io connect-mongo | |
*/ | |
var express = require('express'); | |
var app = express(); | |
var server = require('http').createServer(app); | |
var io = require('socket.io').listen(server); | |
var MongoStore = require('connect-mongo'); | |
var redis = require('socket.io/node_modules/redis'); | |
var store; | |
var sessionDBConfig = { | |
db: mgl.pathname.substr(1), | |
host: mgl.hostname, | |
port: Number(mgl.port), // optional, default: 27017 | |
username: mgl.auth.split(":")[0], // optional | |
password: mgl.auth.split(":")[1], // optional | |
collection: 'test_mySessions' // optional, default: sessions | |
}; | |
var mongoStore = MongoStore(express); | |
var cookieHandler = express.cookieParser("secret"); | |
app.configure(function() { | |
app.use(cookieHandler); | |
app.use(express.bodyParser()); | |
app.use(express.session({ | |
secret: 'secret', | |
key: 'express.sid', | |
/*cookie: { | |
maxAge: 60000 * 20 | |
},*/ // 20 minutes | |
store: store = new mongoStore(sessionDBConfig) | |
})); | |
var pub = redis.createClient(rtg.port, rtg.hostname); | |
var sub = redis.createClient(rtg.port, rtg.hostname); | |
var Client = redis.createClient(rtg.port, rtg.hostname); | |
pub.auth(rtg.auth.split(":")[1]); | |
sub.auth(rtg.auth.split(":")[1]); | |
Client.auth(rtg.auth.split(":")[1]); | |
var RedisStore = require('socket.io/lib/stores/redis'); | |
io.configure(function() { | |
io.set("transports", ["xhr-polling"]); | |
io.set("polling duration", 10); | |
io.set('store', new RedisStore({ | |
redisPub: pub, | |
redisSub: sub, | |
redisClient: Client | |
})); | |
}); | |
server.listen(process.env.PORT, process.env.IP); | |
}); | |
app.get('/', function(req, res) { | |
res.send('<html><head>'+ | |
'<script src="/socket.io/socket.io.js"></script>'+ | |
'<script src="//code.jquery.com/jquery-latest.js">'+ | |
'</script><script>$(function () {var socket = io.connect(), input = $("#input");socket.on("recv value", function(value){$("#lastData").text(value);});'+ | |
'input.keypress(function (e) {if (e.which === 13) {socket.emit("set value", input.val());'+ | |
'input.val("");$("h2").text("value set, reload page");}}).focus();});</script>'+ | |
'</head><body><div id="lastData"></div><input id="input"></body></html>'); | |
}); | |
io.set('authorization', function(data, accept) { | |
cookieHandler(data,null,function(){ | |
data.sessionID = data.cookies['express.sid'] || data.signedCookies['express.sid']; | |
store.load(data.sessionID, function(err, session) { | |
if (err || !session) return accept('Error', false); | |
data.session = session; | |
return accept(null, true); | |
}); | |
}); | |
}).sockets.on('connection', function(socket) { | |
var sess = socket.handshake.session; | |
var sessionRoom = 'session-' + socket.handshake.sessionID; | |
socket.join(sessionRoom); | |
socket.log.info('a socket with sessionID', socket.handshake.sessionID, 'connected'); | |
socket.on('set value', function(val) { | |
sess.reload(function() { | |
sess.value = val; | |
sess.touch().save(); | |
io.sockets.in(sessionRoom).emit('recv value', sess.value); | |
}); | |
}); | |
sess.reload(function() { | |
if(sess.value) | |
socket.emit('recv value', sess.value); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment