Aug 252012
 

Excited about version 3.x of Express, the Node.js web framework? It has some great new features, and is certainly worth migrating to. Many of my apps make use of Socket.io, one of my favorite Node modules, and in this case, and as of now (Socket.io v9.1) the migration isn’t as smooth as it could be. Until socket.io catches up with the changes to Express, here’s how you can use it with Express 3.x.
Back in Express 2.x, you would create an express object:

var express = require('express');
var app = express.createServer();

and then configure your socket.io object to listen along with the express app:

var io = require('socket.io').listen(app);

then tell the express app to listen on a port:

app.listen(8080);

One of the first changes in Express 3.x is that express.createServer() has been deprecated. If you try to use it, you will receive the following message when you start your app:

Warning: express.createServer() is deprecated, express applications no longer inherit from http.Server, please use:
var express = require(“express”);

var app = express();

Ok, no big deal, let’s just do exactly what it says:

var express = require('express');
var app = express();

Want to master Node.js? Try my highly-rated online course Learn Node.js by Example. Click here to get 50% off on screencasts, interactive projects, and more!

However, you now have a very different object stored as ‘app’ then you did in Express 2.x. Before, app was derived from the HTTP server object type, which is what socket.io expects to be passed in its listen() method:

var io = require('socket.io').listen(app);

If you pass this new app object in, while no error will be thrown, the socket.io connections will not succeed. On launching your app, you will even get a helpful message:

Socket.IO’s `listen()` method expects an `http.Server` instance as its first parameter. Are you migrating from Express 2.x to 3.x?
Which is a pretty good clue that you need to make some minor adjustments. Here’s how to fix things.

First, create an HTTP server object from your new Express 3.x app object:

server = http.createServer(app);

Now, pass this server object to socket.io instead:

var io = require('socket.io').listen(server);

Finally, you will need to invoke the server object to start listening on a port, rather than the app object, since the server object is what socket.io is linked with. This will work fine, but is against the standard Express 3.x convention:

server.listen(8080);

Keep an eye out, as you this is a change you will have to make for the time being, especially when adding socket.io into existing Express 3.x apps.

Happy Noding!

  • Benjamin D42

    server = http.createServer(app);
    ^
    ReferenceError: http is not defined

    • http://www.codehenge.net Constantine Aaron Cois

      You have to require the http module:

      http = require(‘http’)

    • Sapardi

      You’re right Aaron, Benjamin has forgotten to include ‘http’ module in his code.

  • Willian12345

    helpful ,thanks

  • Gut6

    Uf, You helped me a lot! Thanks for this article.

  • StuffAndyMakes.com

    An hour of searching for why I was getting a silly 404 on the client side when trying to bring in the /socket.io/socket.io.js file and you described the answer right here. Sooooo glad I stumbled onto this! THANK YOU!

    • http://www.codehenge.net Constantine Aaron Cois

      Glad to be of help!

45 queries in 0.861 seconds