424 CHAPTER 10 WebSocket communications
Socket.IO has a client-side and server-side library; the client side uses JavaScript, and the
server side is JavaScript for use with Node.js. It uses feature detection to decide whether the
connection will be established with WebSocket, AJAX long polling, or Flash polling, which is
yet another way to implement polling but requires Flash at the browser.
The Socket.IO library is easy to use and is used in the exercises to create a chat server.
Lesson summary
■■The WebSocket protocol provides a standardized way for the server to send content to
the browser without being solicited by the client, and it allows messages to be passed
back and forth while keeping the connection open.
■■The WebSocket object contains methods to send data and close the connection.
■■The WebSocket object contains the following events: onclose, onmessage, onerror, and
onopen.
■■You can check the readyState property on the WebSocket object to obtain the state of
the connection.
■■Use ws:// for WebSocket protocol or wss:// for secure WebSocket protocol.
■■Timeouts, dropped connections, web farm implementations, and browser incompat-
ibility are problems you must resolve when implementing WebSocket.
Lesson review
Answer the following questions to test your knowledge of the information in this lesson. You
can find the answers to these questions and explanations of why each answer choice is correct
or incorrect in the “Answers” section at the end of this chapter.
- When working with the WebSocket object, which event can be used to retrieve the
data that was received from the server?
A. onopen
B. onclose
C. onmessage
D. onerror - Which library would you use if you are creating a Node.js website and want to write
browser-independent code that uses WebSocket?
A. SignalR
B. Socket.IO
C. FarmSockets
D. AgnosticSocket