O'Reilly logo

Building Web Applications with Erlang by Zachary Kessin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Note

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 Socket.io (http://socket.io) and the Erlang package socket.io-erlang (https://github.com/yrashk/socket.io-erlang).

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required