I finally got to spend a longer while with Pedestal. It is a pure Clojure web framework, where by pure I mean that you’ll see Clojure on back- as well as front end. For starters I only focused on the front end and that’s what this post is about.
What Is Pedestal?
The documentation is good, but it only explains the fundamental concepts. I think it lacks practical examples and more exhaustive API docs (like inputs and expected outputs of each function, options for each configuration etc.). Or maybe it lacks a tutorial (if I win some time on a lottery, I might write one). There are two good sample applications in their repository: helloworld and chat. Unfortunately, helloworld is very simplistic, and chat is a little bit too complicated for beginners. It does a great job showing off all of the features, but it was a little bit too much for me.
So, after a good while of reading the documentation and reverse engineering the samples, I think I’m finally starting to understand what Pedestal is all about.
On the client side Pedestal is a purely functional, reactive, message-oriented framework. All the state is defined in two pieces: the data model and application model. On the surface each of those models is just a single Clojure map, effectively a tree.
Most of the time you don’t operate on the state directly. You define functions (transforms) that react to messages on specific topics and get current state of data model as argument. They return the new value of the state, and the framework will make it the new data model. Another essential kind of function is emitter: In response to certain messages it emits commands (deltas) to update the application model (like “enable transform”, “disable transform”, “create node”, “set value of node”). Finally, in the application level, you can define functions that change something in the behavior or in DOM in response to those deltas.
Everything goes through this pipeline. If an event happens in GUI, or anywhere in the application, it appends a message to the queue and kicks off all the transforms that bubble up from data model through application model to DOM.
This is a simplistic view that doesn’t do justice to all the features, but I think it’s a good starting point.
Better Sample App for Beginners
During this exploration I wrote another sample application. It’s yet another reincarnation of the TODO app that has a single text entry and a list of everything that has been entered so far. It’s much simpler than the chat demo, and it has plenty of comments explaining what’s going on. This is the kind of application that would help me tremendously during in the learning process, and I hope it saves someone some trouble. You can find the source at Github.
Verdict
My initial verdict of Pedestal? It’s definitely worth a close look! I haven’t built any real apps with it yet, but it looks very promising. It’s a breath of fresh air – I’ve never seen anything similar, especially on the client side with JavaScript. And it feels very appropriate: composed of clean, small, well-defined and isolated pieces with sane flow of information. It appears to lead to much cleaner codebase than JS frameworks like Knockout or Angular with their shared state and dangling functions and callbacks everywhere.
Thanks for this. I was looking for something exactly like this when looking into Pedestal, and decided to hold off and wait on looking deeply at the framework. Now I’ll be looking at it again!
Thanks for writing this post and especially todo app (and comments there)! It’s so much easier to track what’s going on than in their chat app or in the results of ‘lein new pedestal-app’.
Thx a lot for sharing your experience.
Have you seen this implementation https://github.com/rkneufeld/samples/tree/todo-mvc/todo-mvc ?
I think that it would be very interesting to compare the things that you decided to implement differently in order to learn about the various tradeoffs.
Cheers,
B.
I haven’t seen it, and I will take a look. It seems to be a bit bigger than my trivial example.
Hey Konrad,
Thanks for the post, it was excellent. I just wanted to post and say I’ve taken my todo-mvc sample off my samples fork; it was honestly not my best work and represented a lot of trial and error while learning Pedestal (mostly error) – definitely not a good place to see best practices.
Thanks Ryan. I still haven’t gotten to viewing that sample yet.
In perspective I think the chat application is really quite good, but too much for an absolute newbie (especially without docs).