Created
August 7, 2012 20:00
-
-
Save slickplaid/3288839 to your computer and use it in GitHub Desktop.
Multi-domain Socket.io
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
node_modules/ |
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
/* /opt/app1/app.js */ | |
var express = require('express'); | |
var app1 = express(); | |
// if you need to do something specific to app1 with sockets | |
// you have an array of them in this variable | |
var sockets = require('./socketControl').sockets; | |
// we can require main.js again to grab those exported objects | |
var main = require('./main'); | |
// app1 logic here | |
app1.all('*', function(req, res) { | |
var site = req.headers('Host'); | |
res.send('You are on '+site); | |
}); | |
exports.app = app1; |
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
/* /opt/app2/app.js */ | |
var express = require('express'); | |
var app2 = express(); | |
// if you need to do something specific to app2 with sockets | |
// you have an array of them in this variable | |
var sockets = require('./socketControl').sockets; | |
// app2 logic here | |
app2.all('*', function(req, res) { | |
var site = req.headers('Host'); | |
res.send('You are on '+site); | |
}); | |
exports.app = app2; |
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
/* /opt/index.js */ | |
var express = require('express'); | |
var main = require('./main'); | |
// load configuration | |
main.configure(); | |
var app = main.app; | |
var db = main.db; | |
var io = main.io; | |
// once it completes, we'll have access to | |
// db, io, and app from main variable | |
app.use( express.vhost( 'domain1.com', require('./app1.js').app )); | |
app.use( express.vhost( 'sync.mysite.com', require('./app2.js').app )); | |
// put this route in for testing since im lazy | |
app.all('*', function(req, res) { | |
var host = req.header('Host'); | |
res.send('Your host header is: '+host); | |
}); | |
main.server.listen(3000); |
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
/* main.js */ | |
var http = require('http'); | |
var express = require('express'); | |
var socket = require('socket.io'); | |
var mongo = require('mongojs'); | |
module.exports.configure = function() { | |
var app, io, app1, app2; | |
var app = express(); | |
var server = http.createServer(app); | |
var io = socket.listen(server); | |
var db1 = mongo.connect('app1'); | |
var db2 = mongo.connect('app2'); | |
app.configure('production', function() { /* do stuff */ }); | |
app.configure('development', function() { /* do stuff */ }); | |
io.configure('production', function(){ | |
io.enable('browser client etag'); | |
io.set('log level', 1); | |
io.set('transports', [ | |
'websocket' | |
, 'flashsocket' | |
, 'htmlfile' | |
, 'xhr-polling' | |
, 'jsonp-polling' | |
]); | |
}); | |
io.configure('development', function(){ | |
io.set('transports', ['websocket']); | |
}); | |
/* ...etc... */ | |
// export after configuration | |
module.exports.app = app; | |
module.exports.io = io; | |
module.exports.db1 = db1; | |
module.exports.db2 = db2; | |
module.exports.server = server; // need this for 3.x compat of socket.io | |
}; | |
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
{ | |
"name": "socket.io test", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"express": "3.x.x", | |
"mongojs": "x.x.x", | |
"socket.io": "x.x.x", | |
} | |
} |
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
/* socketControl.js */ | |
var io = require('./main').io; | |
var sockets = module.exports.sockets = []; | |
io.sockets.on('connection', handleConnection); | |
io.sockets.on('disconnect', handleDisconnect); | |
function handleConnection(socket) { | |
// do stuff | |
sockets.push(socket); | |
}; | |
function handleDisconnect(socket) { | |
// remove disconnected sockets | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment