O'Reilly logo

CoffeeScript Programming with jQuery, Rails, and Node.js by Michael Erasmus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

WebSocket on the server

We have our client-side code ready to make WebSocket requests; now we can move on to our Node backend. First, we'll need to set up our Socket.IO middleware. There is a small caveat to this, in that we cannot use Socket.IO as a middleware of the Express application directly, since Socket.IO expects a Node.js HTTP server and has no direct support for Express. Instead, we'll create a web server using the built-in Node.js HTTP module, passing our Express application as requestListener . We can then use the listen function in Socket.IO to connect to the server.

The following is how the code looks in our app.coffee file:

express = require 'express'
path = require 'path'

app = express()
server = (require 'http').createServer app ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required