(UPDATE) For anyone looking for an easy way to integrate
socket.io into keystone, this is what I recommend. First, install '
express.io' for your site. This will also install
socket.io as one of its dependencies:
npm install
express.ioNext, make your 'keystone.start' look like this:
keystone.start(
{
onHttpServerCreated: function()
{
// Instantiate an express.io app object, tack it on to keystone
keystone.socketioapp = require('express.io')();
// The 'server' property is used internally by express.io as the express http or https object, so copy it from keystone
keystone.socketioapp.server=keystone.httpServer;
// Since the http(s) object has already been created by keystone just before this callback, we call express.io's socket.io instantiator rather than creating another server with: keystone.socketioapp.http().io(); This allows socket.io to use the same port as keystone.
keystone.socketioapp.io();
// Setup your routes before 'listen' is called. This example 'ready' route will emit a 'talk' event back to the client.
keystone.socketioapp.io.route('ready', function(req)
{
req.io.emit('talk', { message: 'io event from an io route on the server' })
});
// A little sugar to make our example play a bit nicer
var port = keystone.get('port');
// 'listen' will share the port with keystone
keystone.socketioapp.server.listen(port?port:3000);
}
});
The
socket.io object in our example above is available anywhere in keystone as 'keystone.
socketioapp.io'.
For the client, add the
socket.io library to your javascript folder: /your site/public/js/lib/socket-io/socket.io.js. This file will be found in your
socket.io module folder. If you installed
socket.io before installing
express.io, you can find it in: /your site/node_modules/
socket.io/node_modules/socket.io-client/dist/socket.io.js, otherwise it's in: /your site/node_modules/
express.io/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.jsTo test the example route above, add this to the bottom of your default JADE template in /your site/templates/layouts/default.jade, right after the comment "//- Add scripts that are globally required by your site here.":
script(src='/js/lib/socket-io/socket.io.js')
script.
io = io.connect()
io.emit('ready')
io.on('talk', function(data) { alert(data.message) })
After you sign in, the alert message should pop up on every page in your regular views (but not the admin pages).
Express.io has some nice abstraction for handling
socket.io, so look over their
documentation for examples.
JED - would you consider integrating
express.io into keystone? I've attempted this, but there's something in the way keystone handles cookies that causes
express.io to crash in the
socket.io module when doing the handshake. There's two basic steps to add it in. First, change 'express = require('express'),' to: 'express = require('
express.io'),' then change 'keystone.httpServer = http.createServer(app);' and 'keystone.httpsServer = https.createServer(sslOpts, app);' to 'keystone.httpServer = app.http().io().server;' and 'keystone.httpsServer = app.https(sslOpts).io().server;' respectively.