So clojure web development now not a hot topic there is not much info where to start, what to do, what to use to strart, what is clojurescript and how to work with it.
But if we dig deeper there are many tools to use for web development. First thing “Noir” simple framework for web dev, something like Sinatra in ruby world. Monger library to work with MongoDB. Hiccup to make some html code in clojure pretty close to HAML.
We start making simple app and in every step we will use some library to solve the problem.
Short todo list - We start with Noir and make welcome page. Here we find out what need to start simle web app. - Then we start Bookshelf app. Idea is simple suppose you have some books in your home library so we will make a list of that books.
What we can do with it:
Here are articles that already there.
These topics not set in stone they are subject to change. As i evolve in clojure so articles will change. Maybe better to set topics and add something to them like Working with DB ( working with MongoDB, working with MySQL, Postgre or Datomic) will see later. All suggestions for improvement are welcome. If you have something to say i am happy to hear from you, different point of view or important notice is welcome.
Ok let’s imagine that we have 100 friends that use our list and they are all adding books to the list now. We can refresh the page to see results or pushing F5 each minute but that’s boring and contrproductive and not smart ! Let’s make our app update list itself if someone added a book.
Demo
As usual i will show the simpliest way possible. We will use pusher.com service and clj-pusher lib to make our site realtime. We need to add this code to our app.cljs:
This will hook us to pusher service. Ok we subscribed to the channel and using 1 event (my_event).
As we add a book we trigger event at the server:
At client side we have listner for events with:
(.bind channel “my_event” (fn[data](callbk data)))
As pusher-event function executed browser got a message and callback is applied.
We used id to retrieve last book and prepend it to the list.
That’s it code on github is there.
There are some options for implementing realtime functionality on the site. For example we can use:
Next time i will use shoreleave lib to make alternative implementation. Stay tuned.
So we have made Bookshelf app and need to show it to our friends how to do this ? You need just 2 simple steps.
Add this to your project.clj file:
Heroku will use your production database, so it will run something like lein run.
Ok then
heroku create
git push heroku master
And our app is there.
Couple of important notes.
This time we well upgrade our app with picture uploading feature.
There is 2 ways to work with pictures
Pluses and minuses of first way:
Second way:
Mostly using remote service is good for small apps or for prototypes that handles 100 to 1000 pics a day. Maybe things will change soon but comparing and analysing these two ways is topic of another article.
You can write all client side js in JavaScript and with any js framework but there is an option. ClojureScript - javascript in clojure. You can write all your javascript in clojure. It’s not very difficult let’s see some examples.
(defn hello-world [] (.log js/console “hello world”)) ; Will print hello world in console
As you can see we invoke method of the object via .method and pass “hello world” parametr. This will translate to:
function hello_world (){ console.log( “hello world”)}
Notice we can write hello-world in ClojureScript but cann’t do this in js. Property of the object can be invoked like this (.-property object).
If you want to know more there is a book about ClojureScript “ClojureScript Up and Running” O’Reily publishing.
We need someplace to store our data, and so the first thing our project will need is persistence.
I used MongoDB and monger lib for it. There are 2 options that you can use
MongoHQ provides a free “Sandbox” plan with ( 512mb). As i planed to host my app at heroku i choosed to use remote db from the start. Moreover MongoHQ has nice interface to manage your dbs, collections and records. Here is setup steps to use mongohq account.
Create account at MongoHQ.
Create DB at mongohq.
Collection is created when you first time create a record.
Place [com.novemberain/monger “1.3.1”] to your project.clj file in :dependencies section.
lein deps
This will install monger lib.
Ok let’s make some edits to our server.clj file. We will use mongodb uri:
Here i will post series of articles about how to make simple web app in Clojure. I plan to talk about how to start building web app, how to upload it to heroku, how to upload images, use ClojureScript and so on. I am not very experienced programmer and worked mostly with ror framework so any comments and suggestions are welcome.
These notes mostly for beginners like i am. Some day i decided to build prototype for my web project and try new thing like Clojure for it, that was pretty yeasy and joyful.
It took me about 2 weeks to build clojure web app.
You have many options to start web app now days. Some of my favourites:
I worked with ror mostly and just tried to make very simple app with others.
Opinionated note.
They are fine frameworks and languges but all of them praise OOP paradigm. I have no big experience with programming but i can say that this concept is overcomplicated from my point of view. I even think that people just didn’t understand objects concept and started using it in another way, mostly wrong way. I could say that nowadays it’s Class programming.
“Classes orginize your code” ! I saw this phrase on some experienced programmer blog and it really is, that how most people think about this. You have to architect classes and hierarchies of classes to make things work but what about objects ? Any way you have to learn how this stuff works and it takes time, in some cases a lot of it.
Nowadays you can choose between complexity and simplicty. Long long ago was born Lisp. I don’t know why but it was mostly forgotten for years till these days. Clojure mostly reincarnation of this simple and powerfull language and concepts behind that. Now i understand why scheme is the language that people learn at computer since course in MIT. It simple and powerfull and it’s ideal for beginners.
You can learn most vital concepts in Clojure in a day. Syntax in an hour. And you can start doing things rightaway. So did i. I used only functions. No classes no variables no state no class hierarchies and thinking how this stuff works and why.
For simple web app i choosed “Noir web framework”. Most recent version 1.3.0-beta10 as of time this writing. It lucks good documentation and examples but is enough for start.
In order to compile and run your app you also need Leiningen. I used 2 version.
So what you need to do.
lein new noir mywebapp

It using noir template for the project.
$ lein new [TEMPLATE] NAME # generate a new project skeleton
We get simple structure for our web app and can run it.