I've been playing around with Scala a bit in the last few months so this was a good opportunity to try Lift, a web framework written in Scala. In the end it turns out that very little code was needed to create an auto-updating counter. There are three components:
- We've got a DBUpdater object that uses Lift's Schedule (used to be called ActorPing which caused some confusion for me) to update its internal count of raw occurrence records every ten seconds. The beauty is that there is just one instance of this no matter how many clients are looking at the webpage.
- The second part is a class that acts as a Comet adaptor called RawOccurrenceRecordCount which waits for updates from the DBUpdater and passes these on to the clients.
- The last part is the Bootstrap code that schedules the first update of the DBUpdater and sets up the database connection and other stuff.
To get to this point, though, took quite some time as I have to say that the documentation for Lift is very lacking especially in explaining the basic concepts (I've read Simply Lift, bits and pieces in the Wiki and am halfway through Exploring Lift) for beginners like me. I'm really looking forward to Lift in Action and really hope it serves as a better introduction than the currently available documentation.
That said I liked the end product very much and I hope to be able to extend the work a bit more to incorporate more stats for our wallboard display but so far I haven't managed to call JavaScript functions from my Comet Actor. That's next on my list. Ideas for a wallboard are piling up and I hope to be able to continue doing it in Lift and Scala.
Nice work, Lars. Can you see any advantage over a jquery timer pulling from a plain servlet or jersey service that has some singelton or static class updating its count anytime its outdated?
ReplyDeleteThank you!
ReplyDeleteAdvantages: I didn't have to write a single line of JavaScript myself. All the Comet handling stuff was done for me, no need to deal with synchronization stuff updating a counter either, no need to query a servlet every few seconds as updates will be pushed to the clients, easy scheduling of a task (the count updating) and I learned a bit of Lift :)
That said it obviously could have been done using "conventional" means and considering the learning curve probably quicker as well.