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.