Chapter 22

Making Universal Real-Time Web Applications Using Socket.IO

WHAT’S IN THIS CHAPTER?

  • Understanding the history of HTTP in relation to real-time applications
  • Understanding how WebSockets enable real-time applications on the web
  • Using Socket.IO to write real-time applications for Node.js
  • Writing a web-based real-time chat with Socket.IO for Node.js
  • Using namespaces to separate Socket.IO apps
  • Scaling to several Node processes with Redis

For many years, HTTP and real-time applications didn’t really go together very well. The reason lies in the very nature of the HTTP protocol, which is stateless and based on a request-response cycle – an HTTP server only reacts upon requests from the client but doesn’t have a stateful continuous connection to the client.

An HTML and JavaScript-based application is capable of reacting to client events and sending them to the server, but the reverse is not easy. If an event on the server occurs, the server has no way of actively informing its clients about this event in real time. Only when clients ask the server for its current state can the information be delivered from the server to its clients.

A browser-based chat is a good example of a web-based real-time application that shows the limitations of HTTP. Multiple users share a chatroom, which technically means that a message sent by any of the participating users needs to be delivered to all other users in the same room.

To achieve real-time, or at least near real-time, behavior in traditional ...

Get Professional Node.js: Building Javascript Based Scalable Software now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.