415
Chapter 10 WebSocket communications
CHAPTER 10 WebSocket communications
T
his book has covered AJAX and web services, which provide a means to communicate to
the server from the browser client. This chapter is about initiating communications from
the browser to the server or from the server to the browser. If the server can send messages
to the browser, the browser doesn’t need to poll the server for status information.
An example of two-way communications is a chat room application by which the server
can notify many browsers that a message was received from a browser. Many games send
data to a server, and the server controls data being sent back to the clients. Another exam-
ple is when you initiate an asynchronous operation at the server, and the server provides
status update messages to the browser. This chapter explores bidirectional communications,
using WebSocket.
Lesson in this chapter:
■■Lesson 1: Communicating by using WebSocket 415
Before you begin
To complete this book, you must have some understanding of web development. This
chapter requires the hardware and software listed in the “System requirements” section in
the book’s Introduction.
Lesson 1: Communicating by using WebSocket
The WebSocket protocol is a web technology that provides full-duplex communications
over a single TCP connection. The WebSocket application programming interface (API)
is currently in working draft status by the World Wide Web Consortium (W3C), but the
WebSocket protocol has been standardized by the Internet Engineering Task Force (IETF)
as RFC 6455.
WebSocket replaces the long polling concept. With long polling, the client sends a
request to the server, and if the server has information with which to respond, it responds.
If the server doesn’t have information with which to respond, the server keeps the connec-
tion open and doesn’t respond until it has data. When the client receives the data, it sends
another request to the server, and the sequence repeats itself. The benefit of long polling is
Key
Te rms