O'Reilly logo

Professional Node.js: Building Javascript Based Scalable Software by Pedro Teixeira

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

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 ...

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