Monday, September 28, 2009

Erlang StickyNotes under WebMachine (MochiWeb)


[Edit] The story continues with release 0.2: the application goes RESTful. Plus, you can now turn the webmachine TRACE ON/OFF right from your browser. [/Edit]

Kevin Smith from Hypothetical labs has recently stressed the value of the WebMachine. It happened to be not just yet another "cool" stuff, but the real thing.

Thanks, Kevin!

The best way for me to play with and learn it is to use the real working application. That one happened to be the very nice StickyNotes created by the Hughes Waroquers of BeeBole. Actually, the BeeBole itself has a trailer which looks inspiring and I see where the roots of coolness come from.

The post by Justin Sheehy shows how to integrate the StickyNotes with the original design of the WebMachine. He took the StickyNotes "as-is" and put them under the Webmachine layer.

But the snippets given there which add up to the resource module, do not work with the newest WebMachine releases. They added the wrq module to encapsulate the Request Data manipulation. The calling convention is updated as well.

So, to make it up and go a bit further, I have created a new repository in bitbucket.
It took the StickyNotes client and DB parts "as is" and on the protocol level (as was suggested by Justin):
  • separate static pages and dynamic traffic between different resources
  • restrict access methods for each resource
  • send read requests through GET, not POST (coming soon)

The distribution, when downloaded is ready to launch (use the run.cmd on Windows) . It only needs working Erlang/OTP.

The home page is now a "pure" AJAX web client, allowing to switch GET/POST.... and Content Type and see the results in detail, very much like Curl (not that powerful, but handy).
I left some debug prints on the server to see how the data reflects in the back-end.

The (PUT,Authorization,ETAGS,DELETE) posts of Bryan Fink were extremely helpful to get the concept. Well, POST is not there, but it is here now! And last, but not the least is the the very Justin's screencast, which is was very inspiring for me.

1 comment:

Anonymous said...

Dear Serge

Thank you for this post. I am learning Erlang and the stickyNotes demo, with MochiWeb and WebMachine, are very helpful

I wonder if you could offer some advice.

I am trying to split the stickyNotes app between two servers: one with the html, css, img and js, and one with the erlang server. For now I'm just using the plain old MochiWeb version, as it's clearer what's going on under the hood.

However it's not working.

The only change I've made to the code is in application.js I have changed the calls to post() from

post(json, "/notes", null);

to

post(json, "http://localhost:8000/notes", null);

The erlang server is receiving the requests, but instead of the POST requests it's expecting, it's receiving OPTIONS requests.

I need to figure out what stickyNotes_web.erl should do with an OPTIONS request, and how to get the browser to send POST requests.

I realise this is not really an Erlang question. It's probably more to do with the change in the http handshake caused by the split between two servers. However, is this something MochiWeb and/or WebMachine can handle? Or is this the wrong way to split the app over two servers?

Any help appreciated. Once I've sorted it out I'll post a summary.

Best wishes

Ivan