Chapter 13
Going Online with WebSockets
In This Chapter
• Using WebSockets for online communication
• Introduction to Node.js and server-side JavaScript
• Creating a simple chat application
IN THIS CHAPTER, I introduce you to WebSockets and Node, two relatively new technologies that make creating network applications and server-side scripting a much better experience than it has been in the past.
In the first half of the chapter, I take you through the WebSocket API and show you how to establish and manage a connection to a server as well as how to communicate with the server by sending and receiving data.
I then show you how to use Node to create server-side JavaScript applications. By the end of the chapter, you will know how to create a chat server and client using Node and WebSockets.
Using WebSockets
Traditionally, whenever network communication outside regular Ajax requests was needed, the solution was either plug-in-based or a workaround using existing XmlHttpRequest technology, possibly involving long-lived connections that wait for responses from the server. HTTP wasn't really designed with polling in mind, and HTTP requests and responses carry a lot of overhead. When all the HTTP headers, cookies, and other request data are combined, an HTTP request can easily have 1K of overhead data. That's a lot if you want to send only a short status update.
WebSockets were created to provide an alternative geared specifically toward persistent connections and low-latency communication. ...
Get HTML5 Games: Creating Fun with HTML5, CSS3 and WebGL, 2nd Edition 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.