Misultin: erlang and websockets

Misultin: erlang and websockets

Inspired by Joe Armstrong’s post, I’ve recently added websocket support to misultin v0.4, my Erlang library for building fast lightweight HTTP servers.

Basically, websockets allow a two-way asynchronous communication between browser and servers, filling the gap that some technologies such as ajax and comet have tried to fulfill in these recent years. If you want to try this out yourself, you will first need to grab a browser which implements websockets, such as Google Chrome.

The typical html page with javascript code to use websockets is as follows:

 

Here’s the code to use misultin to handle the requests of this script:

handle_websocket/1 is spawned by misultin to handle the connected websockets. Data coming from a browser will be sent to this process and will have the message format {browser, Data}, where Data is a string(). If you need to send data to the browser, you may do so by using the parametrized function Ws:send(Data), Data being a string() or an iolist().

Compile and run the example here above with misultin_websocket_example:start(8080). Then, open up your Chrome (or other websocket compliant browser) and point it to an .html file containing the above code.

You should normally see this being gradually printed on your browser:

In normal environments you may consider serving the .html page from misultin directly. You may do so with the following and complete misultin module:

Please note that the Websocket Protocol still is draft. use with caution.

9 Comments

  1. David Semeria

    Very clean. Well done.

  2. Thanks for the post.
    I have run the code using the example misultin_websocket_example under example directory, and it works in chrome, but failed in my iPad iOS 4.2, when i open the URL in my ipad, i just got websocket closed, any idea? thanks.

  3. That probably means that your browser doesn’t have websockets enabled.

  4. Thanks for this.
    For other people hitting this, websocket protocol has changed, new browsers (ie: Firefox 7) use the new version, it is now using this specification: draft-hybi

    http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17#section-5.6

  5. Hi gilemon,

    these are already covered in the DEV branch of misultin.

    Cheers,

    r.

  6. Stina Kvamme

    In my fork of mochiweb I have implemented a dispatcher that is starting a new erlang process matching an html page. If you browse to fobar.html fobar.erl is spawned and implements the websocket functions for that page.

    Is that something you already have in misultin?

  7. Hi Stina,

    misultin implements just a static directory, but no, it does not automatically convert REST to filenames.

    it would be a nice addition on top of it, but not as core.

  8. Stina Kvamme

    Ok, it will be another example then.
    Thanks for your good work :)

Trackbacks for this post

  1. Greg DeAngelis - Just Another Software Developer's Blog

Leave a Reply