I've heard of two Clojure based web application frameworks: Webjure and Compojure. Can someone let me know which is better?
Now you can add Ring to the list. All of these frameworks are very new and likely to evolve (or die) quickly, but Compojure does seem to be the most actively developed based on the past 6 months or so.
"Better" is too subjective a question to get a definitive answer to. Try them all and see what works.
Compojure has been working very well for me so far. I like the simplicity of the design, the flexibility and the fact that it encourages a nice idiomatic functional style.
Sample server:
(use 'compojure)
(defroutes my-app
(GET "/index.html"
(html
[:h1 "Hello World!!"]
[:body "This is some text"]))
(ANY "*"
[404 "Page not found"]))
(run-server {:port 80}
"/*" (servlet my-app))
Note that Compojure uses Ring internally.
I second Rayne's recommendation on Moustache.
Right now, we are using Ring (base layer, middleware), Moustache (routing), Hiccup (html generation). We just began using Compass for CSS (http://compass-style.org/). So far, I'm happy with this collection of small libraries rather than a big "complete stack" framework (Django, Rails, ect...).
Now, there is also a new one named Noir build on top of compojure. Really recommended, especially with Heroku.
Compojure seems to be getting the most buzz right now. Not necessarily indicative of quality, but the one with the most eyes will probably evolve the fastest.
There is also Moustache, which is what I use in TryClojure, along with Ring. It's pretty awesome.
Compojure is based on Ring, which is a low-level framework that doesn't attempt to hide much of HTTP. It's similar to WSGI (Python) or Rack (Ruby). Ring has a concept of middleware, small pieces of code that can do something meaningful with an HTTP request and/or response, such as dump header infos, manage cookies etc. Compojure is a higher-level framework, somewhat similar to Ruby's Sinatra. Its aim is to provide a convenient API (or DSL, if you prefer) for most tasks a Web app developer faces. It's usually used together with an HTML generation library, such as Hiccup.
I haven't heard much about Webjure in the last few months, I'm not sure it's in active development (but I'd be interested to find out more). It precedes Ring, AFAICT, which seems to have become somewhat of a standard for Clojure Web frameworks.
I've been building a project for my own use using Compojure and it has worked out great. It doesn't really get in the way very much and lets you focus on what's important, your problem domain. The project is about 1100 lines of clojure just to give you an idea of the size.
Related
Beginner here.
Can I compile an existing Clojure script to run it on the web using ClojureScript?
Let's say that I have a script that prints Hello world in my terminal, and I want to print that text on the browser. Should I rewrite a whole script with ClojureScript syntax, or should I just compile it using another compiler?
(ns clojure-hello-world.core
(:gen-class))
(defn -main [& args]
(println "Hello World"))
(Long answer :P)
Even though Clojure and ClojureScript share a good amount of features, there are some that are specific to one or the other. For example there are no real classes in JavaScript so the :gen-class specification in the ns form doesn't make much sense.
One important fact is that the syntax of both Clojure and ClojureScript is exactly the same, differences have to do mostly with the host VM in which they run (Java VM in the case of Clojure and JavaScript VM in the case of ClojureScript).
There is a list of the differences between the two Clojure implementations here.
There's also a tool called cljx to "write a portable codebase targeting Clojure/ClojureScript". Recently there has been some discussion on the Clojure Dev group around finally implementing feature expressions which would on one hand deprecate the use of cljx but on the other complicate the work that tools have to do to extract information from Clojure source files.
I would start with lein-cljsbuild to get started. This will get you going with a nice edit eval and look at browser loop. It's well worth getting this setup first because it makes learning ClojureScript much more fun. I promise it's worth the hassle. If you need more interactive support the folks in #clojure on freenode are very kind and helpful.
Basically, the Browser executes JavaScript. You compile your ClojureScript code to JavaScript. The Browser loads your JavaScript via an HTML page. So, you have to create an HTML Page and point your Browser at it.
The simplest way I got started was to use Luminous (http://www.luminusweb.net/docs/clojurescript.md).
However, Chestnut (https://github.com/plexus/chestnut) looks promising.
I'm fairly new to clojure/compojure, but really love it. Naturally, started my exploration with Noir stack. Have written a POC app. Then, discovered Liberator -- makes a whole lot of sense. Just wondering, if anybody has ever migrated Noir applications to Liberator before. Any references to articles/blogs that cover this would be highly appreciated.
The goal for Noir and Liberator is slightly different.
In simple words, you would use Noir to create website or web applications, and Liberator to create APIs, by exposing your resources for RESTful access. So to answer your main question, you would not really migrate from Noir to Liberator.
Also nowadays, Noir has actually been deprecated for compojure+lib-noir
Just in case, the main frameworks to build web applications these days in Clojure would be:
Luminus
Pedestal,
(and to a lesser degree) ClojureScriptOne
(and as stated above) pure compojure + libnoir
I was learning most clojure/compojure from the the book http://pragprog.com/book/dswdcloj/web-development-with-clojure which is still in beta. Seems that it's out of date before it even published. The Pedestal seems to be very very interesting and exciting. Though, what do people think, I know it's hard to predict, but what is a possibility of Pedestal becoming a next defacto standard replacing compojure+libnoir? Just have very scarce resources, and trying to make correct choices early in the game. Making simple decision is difficult these days.
I am using clojure with Vertx and can see my request/response roundtrip. However I would like to leverage the Ring framework, which comes with only a Jetty adapter. Does anyone know of a ready made one for Vertx? I am interested in learning how to write my own, but am short of time to research it.
I don't know anything about vertx myself, but you should be aware that Jetty is certainly not the only webserver that ring can interoperate with: it's the only one bundled into ring proper, but a quick google search reveals a handful of other adapters, eg for netty, simpleweb, mongrel2, and finagle. I did find a discussion about vertx/clojure interop on the google group, though. Looks half-baked and perhaps never finished, but worth looking into at least.
I don't believe so - although such adapter would be ultimately cool!
Have you checked http-kit?
It seems to be fairly performance-effective (haven't checked it myself though) and is 99.9% Ring-compatible.
You can also check out. https://github.com/RallySoftware/netty-ring-adapter It is a drop in replacement for Jetty using Netty.
There is a clojure module here thats being actively developed. There is a ring adapter here .
I'm having trouble understanding the point of clojure's Noir library. It seems to be a framework written on top of compojure that renames defroute to defpage and calls it a day. Obviously an unfair simplification, but what exactly does Noir bring to the table? Why would I use Noir instead of plain compojure+hiccup?
EDIT/UPDATE:
Noir is deprecated, interesting write up here: http://blog.raynes.me/blog/2012/12/13/moving-away-from-noir/.
From the author of Noir himself:
Noir isn't really a replacement for [compojure and hiccup], more of an abstraction over them. It was born out of seeing how a real web-app evolved and what was missing from the ring/compojure/hiccup stack as I built http://www.typewire.io. Also, as I mentioned in one of the other comments, I hope that it will serve as the single "package" to start with web development in Clojure, instead of having to try and cobble it together from the pieces that are out there now. By controlling all of it, I can create a much more cohesive and well defined story for helping people get started. I can also share what I've learned about maintaining websites in Clojure and hopefully encourage patterns that avoid some of the pits I fell into.
(emphasis mine)
Noir, the framework has been deprecated by Noir maintener himself.
It suggests to use Compojure and lib-noir :
For new websites, please use Compojure and lib-noir. This is pretty much just as batteries included as Noir itself ever was! You just have to learn how to write routes with Compojure. It’s easy and just as concise as it was in Noir. You don’t have to use ring-jetty-adapter and stuff, just use the lein-ring plugin to start your server. Also, if you took advantage of Noir including hiccup by default, you’ll have to have an explicit dependency on it now. No biggy, right? Right!
Compojure
lib-noir
Compojure is a small framework that generates Ring request/response handlers, where handlers are chosen based on routes you define.
Noir adds functionality for session handling, cookies, templates and partials, an easier way to generate responses, form validation, route filters, encrypted passwords, JSON (de-)serialising and custom status pages. In other words, Noir combines Compojure, Hiccup, and a bunch of other clever stuff in one package.
You can always check the API on the webnoir.org website. There is a lot of added functionality besides defpage.
Note: I'm a backend (Java) developer by trade and work in Clojure in my spare time, so forgive me for my ignorance.
I'm trying to get my head around Clojurescript and how it could potentially fit in with projects I'm working on, or plan to work on in the future. As I've grown up with the "classic" web development mindset (e.g. Clojure running the backend, distributing data to the frontend via JSON to be processed in JS or returning a HTML page for the browser to render), I'm having trouble trying to understand how Clojurescript might make things better than this model.
Could anyone explain to me what the general approach to Clojurescript/Clojure development would be, seeing as the "Clojurescript One" project moniker signifies that application development will be unified under one language (as such)
What tasks would normally be done in the Clojurescript portion of the application?
What tasks would normally be done in the Clojure (e.g. backend) portion of the application?
Any help would be appreciated, or if anyone can point me towards some diagrams or explanations or anything - that would be great too!
I think Clojure/ClojureScript applications will be structured very similarly to X backend technology + JavaScript.
One big benefit with architecting applications with Clojure and ClojureScript - a richer data format than JSON (you can represent hash-maps and sets with arbitrary keys) without losing compactness.
JavaScript is a fine, fine language but ClojureScript offers quite a few benefits. It's semantically simpler (functional), ships with a rich standard library, a robust battle tested application library (Google Closure), and all the benefits you get from the tasteful application of syntactic abstraction via macros.
That said, it's still very much alpha software and the tooling still needs a lot of work.
A bit of background about me, I have developed with Clojurescript, JQuery, Vaadin, Servlets, JSP, and many other web technologies.
1) Clojurescript is much harder to learn than any other web technology I have used as you need, Java, Clojure, Closure (with an s ;), Closure Lib, and Closurescript specific knowledge.
2) Clojurescript doesn't make sense for a small app. It only makes sense when you will have ALOT of client side processing
3) Clojurescript's only use as far as I see is as a better javascript (which is why it is better suited to larger apps) as the minifier part of Clojurescript is available for javascript too
4) Only the client end would be written in Javascript, the server would be in Clojure/Java servlets
Maybe Ganelon micro-framework (which incidentally I am author of) will suit your needs - the execution model is similiar to Vaadin's: server-side Clojure code pushes UI updates to the browser through AJAX/JavaScript, but we don't store application state in session by default.
The demo and docs are available at http://ganelon.tomeklipski.com/
For me, clojure and clojurescript offers cleaner code than mixed stack. There is only one language to think of and code is quite easy to read.
At the backend clojure does things that java usually would do. Input validation, saving to database and above all, implementing business logic. Our backend also validates incoming / outgoing data by types using prismatic schemas.
Frontend in short: We get pretty code using ClojureScript and it is fast to write. We are using ClojureScript version of material-ui when writing UI components. We have to write less code when compared to the JavaScript and I find our UI component code to be easier to read than JavaScript counterpart. One of the main reasons is shorter closing tags and less noise by coding language. Development with ClojureScript is quite fast.
Of course ClojureScript is used for simple imput validation like RegExp for phone numbers etc.
One of the disadvantages of clojure which you have probably noticed is long lines after giving proper names to the functions. I havent found silver bullet how to cope with that.
As dnolen said: ClojureScript is still developing. It is way better now than it was 6 months ago, so you'll have to keep checking its maturity now and then.