The Browser Server Protocol
The Browser server protocol is extremely simple. It makes use of JSON messages sent over a websocket. This fits nicely with both Erlang and JavaScript and makes interfacing the browser to Erlang really easy.
Sending a Message from Erlang to the Browser
To change something in the browser, we send a message from Erlang to the browser. Suppose we have a div in the browser, declared like this:
| <div id="id123"></div> |
To change the content of the div to the string
abc
, Erlang sends the following JSON
message to the websocket that is connected to the browser:
| [{cmd:'fill_div', id:'id123', txt:'abc'}] |
The code in webserver.js
, which is activated when the browser receives a message from a websocket, is a callback ...
Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.