Clojure lab

Clojure web development for beginners. Contacts clojure.lab@gmail.com
  • rss
  • archive
  • Clojure web development

    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:

    1. Add a book with title and author and store that info to DB.
    2. Add picture to the book
    3. Add a book via ajax call and using fetch library
    4. Edit picture of any book in ajax fashion.
    5. Upload our app to Heroku
    6. Working with S3 store.
    7. Using pusher.com for making our app realtime !

    Here are articles that already there.

    • Starting web app in clojure
    • Working with DB
    • Working with ClojureScript
    • Working with pictures uploading
    • Using heroku for hosting
    • Realtime updates with pusher

    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.

    • 6 months ago
    • 1 notes
    1 Comments
  • Making Bookshelf app realtime with pusher

    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

    How to do that

    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:

    • noir-async library
    • shoreleave client-side library
    • or just use Google Closure framework

    Next time i will use shoreleave lib to make alternative implementation. Stay tuned.

    • 6 months ago
    0 Comments
  • Using heroku for hosting

    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.

    1. Heroku doesn’t host static files ! You need host them on other server ( S3 for example)
    2. Heroku provides for free only postgre database in other cases ( like ours) use remote DB like Mongohq.com.
    • 6 months ago
    0 Comments
  • Working with pictures uploading

    This time we well upgrade our app with picture uploading feature.

    There is 2 ways to work with pictures

    • upload them on the local machine and handle all the resizing stuff with special libraries or
    • use remote service that does all this work for you

    Pluses and minuses of first way:

    • ’+’ speed - no need to connect to remote service and wait for uploaded and resized picture
    • ’-’ you need to know what libraries to use
    • ’-’ you will have 1 or 2 ways of uploading pictures
    • ’-’ you need computing power, more pics more power

    Second way:

    • ’+’ no need in any infrastructure, you just upload the picture and get url of it
    • ’+’ about 10 ways to upload picture ( facebook, dropbox and so on..)
    • ’+’ no need to learn how convertion libraries work- you just need to know how to use service
    • ’+’ you need not any computing power but you have to pay for service if you handle lot’s of pictures
    • ’-’ speed because of the connection to remote service
    • ’-’ you need to pay for service if you use it a lot

    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.

    Let’s code something !

    Read More

    • 6 months ago
    0 Comments
  • Working with ClojureScript

    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.

    Upgrade our app with ClojureScript and Ajax.

    Read More

    • 6 months ago
    0 Comments
  • Working with DB. MongoDB and Monger lib.

    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

    • local db or
    • remote service like MongoHQ

    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.

    1. Create account at MongoHQ.

    2. Create DB at mongohq.

    3. Collection is created when you first time create a record.

    4. 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:

    Read More

    • 6 months ago
    0 Comments
  • Starting web app in clojure

    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:

    • yii framework php
    • play in scala
    • ruby on rails

    I worked with ror mostly and just tried to make very simple app with others. 

    Why Clojure. 

    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.

    Clojure web app development. Starting point.

    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.

    • install Leiningen
    • make Noir web app via this command in console

    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.

    Read More

    • 6 months ago
    0 Comments
© 2012–2013 Clojure lab