Skip to content

Instantly share code, notes, and snippets.

@smithcommajoseph
Created August 6, 2013 14:07
Show Gist options
  • Select an option

  • Save smithcommajoseph/6164776 to your computer and use it in GitHub Desktop.

Select an option

Save smithcommajoseph/6164776 to your computer and use it in GitHub Desktop.
the sockets.io example in Rendr (not working)
var express = require('express'),
http = require('http'),
rendr = require('rendr'),
config = require('config'),
mw = require('./server/middleware'),
DataAdapter = require('./server/lib/data_adapter'),
io = require('socket.io'),
ioServer,
webSocket,
app,
server;
app = express();
/**
* Initialize Express middleware stack.
*/
function initMiddleware() {
app.set('json spaces',0);
app.use(express.compress());
app.use(express.static(__dirname + '/public'));
app.use(express.logger());
app.use(express.bodyParser());
/**
* Rendr routes are attached with `app.get()`, which adds them to the
* `app.router` middleware.
*/
app.use(app.router);
/**
* Error handler goes last.
*/
app.use(mw.errorHandler());
}
/**
* Initialize our Rendr server.
*
* We can pass inject various modules and options here to override
* default behavior:
* - dataAdapter
* - errorHandler
* - notFoundHandler
* - appData
*/
function initServer() {
var options = {
dataAdapter: new DataAdapter(config.api),
errorHandler: mw.errorHandler(),
appData: config.rendrApp
};
server = rendr.createServer(app, options);
ioServer = http.createServer(app);
}
/**
* Start the Express server.
*/
function start() {
var port = process.env.PORT || config.App.port;
webSocket = io.listen(ioServer);
app.listen(port);
console.log("server pid %s listening on port %s in %s mode", process.pid, port, app.settings.env);
webSocket.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
}
/**
* Here we actually initialize everything and start the Express server.
*
* We have to add the middleware before we initialize the server, otherwise
* the 404 handler gets too greedy, and intercepts i.e. static assets.
*/
initMiddleware();
initServer();
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment