Chapter 6. WebSockets

Traditionally HTTP is not very good for live communications. The communication stream is controlled by the client and is really designed for the case where the client wants to load or set data from time to time. A number of methods have been used to simulate TCP-socket-like behavior over HTTP but none of them works very well. HTML5 introduced the idea of WebSockets, a full-on, bi-directional communication channel between the browser and a server.

In some ways WebSockets take the opposite approach to dealing with interactions between the browser and the client than REST does. REST is built around the idea that the browser (or other client) will send a number of discrete requests to the server of the form, show this data, or perform some action.


As WebSockets are not supported in all browsers, having a cross-platform way of handling communication would be helpful. This can be done with the JavaScript package ( and the Erlang package (

From an Erlang perspective, WebSockets make interactions between the browser and Erlang applications more transparent. An Erlang application will generally consist of a bunch of little servers passing messages around between them. When opening up WebSockets between the user’s browser and the Erlang application, we can push that model out onto the user’s browser.

To visualize this, look at Figure 6-1. Here the Erlang cloud on the left consists ...

Get Building Web Applications with Erlang now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.