Two beautiful programs - or web programming made easy

Joe Armstrong erlang@REDACTED
Sat Feb 12 12:33:36 CET 2011

Two beautiful programs

For a long time one of my favorite Erlang program was this:

loop() ->
        F -> F(),

It's nice because it does very little, but what is does is universal. It
enables mobile code.

Well now I can do this in Javascript.

The Javascript equivalent is:

   function onMessage(evt) {

Where the data comes from a websocket.

Websockets are controlled with a simple API:

      websocket = new WebSocket(wsUri);
      websocket.onopen = function(evt) { onOpen(evt) };
      websocket.onclose = function(evt) { onClose(evt) };
      websocket.onmessage = function(evt) { onMessage(evt) };

Linking a websocket and Erlang is pretty easy. So now I can write code like
this in


start(Pid) ->
    Pid ! {eval, "document.body.innerHTML='';"},
    Pid ! {eval, "'#eeffaa';"},
    Pid ! {eval, "document.body.innerHTML+='<h1>Hello World</h1>'"},

event_loop(Pid) ->
    Any ->
        io:format("??event loop:~p~n",[Any]),

This code pushes asynchronous messages containing javascript to a generic
web page, and the web page evals the result.

This technique is amazingly powerful.

So now I only need one generic web page. Think of that.

Only one page is needed - forever.

All the generic pages does is:

   wait for a message containing Javascript
   eval the message


This is the easiest way to program a GUI I can conceive of.

You can download and run the examples from:

You'll need something like Google chrome to run them.

Have fun


More information about the erlang-questions mailing list