Book Image

Node Web Development - Second Edition

By : David Herron
Book Image

Node Web Development - Second Edition

By: David Herron

Overview of this book

Table of Contents (17 chapters)
Node Web Development Second Edition
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Initializing Socket.IO with Express


Socket.IO works by wrapping itself either around an HTTPServer object, or around an Express application object. To understand that, think back to Chapter 4, HTTP Servers and Clients – A Web Application's First Steps, where we used the HTTP Sniffer to view the events emitted by the HTTPServer object. The HTTP Sniffer attaches a listener to every HTTP event, to print out the events. But what if you used that idea to do real work? Socket.IO uses a similar concept, listening to HTTP requests, and responding to specific ones to use the Socket.IO protocol to communicate with client code in the browser.

Adding the Socket.IO library to an Express application requires just a few simple changes, which we're about to look at.

To get started, make a duplicate of the Notes application used in Chapter 7, Multiuser Authorization, Deployment, Scaling, and Hosting, and then follow along with these changes.

In package.json add this dependency:

 "socket.io": "~0.9.x"

This brings...