248 Chapter 9—WebSockets
Summary
With WebSockets, a new protocol has stepped onto the WWW stage. By no means
does this spell the end of the Hypertext Transfer Protocol. The WebSocket proto-
col was developed for special applications where bidirectional communication
between client and server with little overhead is required.
Both the server-side and the client APIs are very easy to program, as you can
see from our first example of a rudimentary chat application. A full-blown, mul-
tiuser, online game is presented in our final example, Battleships!. Here, too, the
communication between client and server can be programmed with a few lines
of JavaScript code, and less code always means less risk of errors.
The introduction of WebSockets makes it easy to program web applications that
previously could only be realized very laboriously via XMLHttpRequests or by
constantly reloading web pages. Large amounts of rapidly changing data can
then be monitored through one website; stock exchange data is just one example.