Lift CometActor with Growl-Style Notifications

So i always wanted some real-time way of letting my users know what’s up. Coming from the request-cycle world, this means a lot of queueing and storing and retrieving. Just for clarification, this is also again a Beginner Howto for using Liftweb‘s amazing Comet capabilities.

To get started, simply start integrating Gritter into your project. It’s a jQuery Growl-Style notification plugin which has nice visuals. Kudos to jboesch!

Anyway, once you’ve got the .js and .css included, let’s start by adding the NotificationMessage:

This simply gives you the opportunity to call .toJsCmd to make it render with all the options provided.

Here is the NotificationCenter:

This is pretty straight forward. It sends out notifications it received from various backend-actors to all registered clients.

The receiving CometActor is dispatching, if the Message has a customer assigned (Long), and if the User this CometActor belongs to is a Member of the provided Customer-ID, then it will be shown. If no Customer-ID is supplied, it will be shown on all clients. So you have some kind of Broadcast-Feature. This also means, that your User-Model needs some sort of “customer”-Field, but feel free to adapt! :)

To implement it, simply use something like this:

(The Users.isLoggedIn is something of mine, so ignore it if you don’t have something similar.)

Now we need some way to send messages, since we don’t have an Actor messaging us yet, so i’ve created a little snippet:

and a form:

which should help you get started.

If you’re lucky and everything works right, you should see something like this:

Sending Form

So i hope this helps some of you newbies out there get started!

n8i! :)

Using LiftActor as ORM-Read-Cache

I’ve been reading a lot lately about how people switch back from Scala to Ruby, since they often fail to adapt to the way of thinking required to make Scala or partially Liftweb scale to the level they want.

I will write a post about that phenomenon and what’s wrong with it a few weeks, still writing that particular piece :)

Anyway, the focus of this post is the LiftActor.

It is a lightweight concept adapted from Erlang’s Actors (Scala has Actors as well, just to mention it), but David Pollack drizzled a little bit of sugar on top.

So what about it?

In my passion project, i’ve come across a lot of listings which require fetching the same resource over and over again. Imagine a User-class with a MappedLongForeignKey for the reference to a Customer-class. If we had a listing of users with a Customer column, it would have to select every Customer again and again for every single User. Depending on the number of Users (or whatever object references your cached entity), this
concept just looks stupid and like the waste of a lot of CPU-cycles to me.

Now for my concept, i simply overrode a def like this:

For simplicity (and since the Customer-table is not that big), i’ve taken the liberty of pre-caching all Customers in my LiftActor:

What does it do? On startup, i will call CustomerCache.init somewhere in Boot.scala to start preloading. You can either send a Customer to the Actor, so it will be stored, you can send a Long in order to get the desired customer, or you can send a CustomerAll which will give you all the Customers. I’ve also taken the liberty of adding roles to my User-class, so i can do ACLs pretty nice (as you can hopefully see). ;)

That’s not all, as a nice extension i’ve added a couple of methods to CustomerCache, so it can be used in Boot.scala’s SiteMap as seen here:

Now we’ve got one problem left, once a Customer gets changed, how does the cache get notified? Well this is pretty easy:

This might not make that much sense on a User-class, but i’ve had the pleasure of reducing page-render time from a 2k-Domain-Listing from ~4200ms to ~80ms. Now that’s what i call performance!

I know that this is not groundbreaking, but i hope this will help getting beginners/people coming from other languages to realize the potential and the possibilities you can have in Web-Development thanks to Actors.

best regards!

Mapper with PostgreSQL custom types CIDR/MAC

This is just a quick one, since i’ve already gotten 3 mails about how i used PostgreSQLs CIDR and MAC Types.

It’s pretty easy, here are the two classes, basically treat it like strings. Haven’t come around yet to implement own query parameters for this (like << or >>) but at least you can store that datatype. If querying is needed, use an insecure query.

A polyglots tail - My Scala LiftOff Talk

Today i held my talk at the Scala LiftOff 2011 in London. It was a very interesting event and i’ve met lots of interesting People. Before this event i could hardly imagine myself presenting, but i hope i did my job to everybody’s satisfaction.

Anyway, i’ve written a small project for this talk, it’s called lift-akka-example and can be found @ http://github.com/fbettag/lift-akka-example.

The Podcast can be seen here http://skillsmatter.com/podcast/scala/liftweb-in-the-real-world-a-hyper-polyglots-tale.

Have fun, check out the source. I’m heading back to Amsterdam.

Scala LiftOff 2011 London

A few weeks ago, i’ve written an article on How Liftweb changed my view of Web-Applications. Well since i am one lucky duck, i’ve gotten invited as a Speaker to the Scala LiftOff 2011 London by David Pollak which will occur from 13th to 14th October at Skills Matter.

What am i going to talk about?

Well i am preparing a project which integrates a Liftweb CometActor with an Akka RemoteActor which is going to be an example of how to use those two together, and how easy it can be to write highly concurrent and typesafe systems.

It’s going to be a small project, simply to show with how few lines of code you can actually achieve something really interactive for users. So don’t expect a humungus amount code, expect slick functional code. I still hope it’s worthy presenting, but i’ll do my best!

The project will be hosted on github once it’s finished, but i’ll write in detail about that in October!

Tickets

If you don’t have a ticket yet, you should better get one! Check the official site Scala LiftOff 2011 London for Tickets and how to get there.

I’ll write all the fancy shit about it once i’ve held my talk, so you better watch this space or my Twitter account. ;)

Keeping track of git repos with your changes on top

This will explain, how you can keep your own changes on top of somebody elses git-repository. We will utilize git rebase for this.

Here i’ll use a bogus project which does not exist, but you can paraphrase from it.

First you clone the repository:
git clone git@github.com:foobar/baz.git

After that you change into the repository:
cd baz/

Now we create our own branch which tracks master:
git branch -t mybranch

Since we don’t want to modify master, we check out the new branch:
git checkout mybranch

And since it helps me, i often remove the local master-branch:
git branch -D master

Now whenever you make changes, you simply commit them to your mybranch branch. When you’re done and your upstream has released a new version, you simply rebase like this:
git rebase origin/master

Conflicts

If there are conflicts, it will ask you to resolve them manually if it is not able to do so by itself. If this happens, simply edit the file so that the conflict is resolved and add it with git:
git add conflicted/file.c

When you’re done editing, simply run:
git rebase --continue

Saving your changes to your own Repository

I find it useful to save my changes somewhere else than my workstation, not only for backup-purpose, but more or less access control, if i ever have to team up.

git remote add mygit git@mygit:myrepo.git

and start pushing to it

git push mygit +mybranch

Hope you find it useful.

Cheers!

How Liftweb changed my view of Web-Applications

First i want to talk a little bit about my Journey with web frameworks, since i’ve used quite a number of them, namely Django, Rails, Tapestry5, Wicket, CakePHP (lol), Play, Merb (my favorite for Ruby) and of course Liftweb.

It all started in 2004 when i first started out doing Web-Development with PHP and MySQL. I mean you could hardly call it development, but i produced an administrative interface for my housing business. Within the first few months i noticed that MySQL was in fact not usable for production, so i quickly became fond of PostgreSQL.

Ruby Era

After figuring out, that PHP has some OOP functionality like class/private/protected/public where the last 3 don’t really do anything except look neat in the code, i started looking for real programming languages that took what i coded seriously, not just for style. That’s around the time i switched to Ruby, that was Winter 2006. I know that because i remember bringing my Ruby On Rails Book to our ski-vacation to Kitzbühel, Austria.

But you know how it is, once you change your Language, you’ll still have a few projects lurking in the back of your head which you did in PHP or whatever your old language was, so you gotta code that shit, wether you like it or not. But i was in (good or bad?) luck, my biggest PHP customer went broke, so went his Servers and his Project. That was somewhat in mid-2007.

My biggest Ruby Project

Around Mid-2007 I struck a deal with Quelle GmbH, a large German warehouse store. The deal was to sell the Quelle GmbH goods through my company; Franz Bettag - Innovative Lösungen. This was a pretty heavy duty job, since it involved sending 40.000 Packages annually to people within Germany. Not to mention my employees collecting the products out of our 2000sqm (~21.500sqft) warehouse, bringing them to the desk to be packaged and having my Warehousing system print invoices and packaging-lists.

So how did i manage this? Well i wrote my own Warehousing System in Rails within 3 Months. It fully interoperated with eBay and did everything they asked for with great ease.

From there on until Christmas 2009, i’ve spent a considerable number of hours writing Ruby for different projects. Most of them non-public, some were a leap of faith which did not produce any good outcome for me or my customer, so the only ruby project i can be proud of is my Warehousing, since it was quite enterprise grade. ;)

What changed

After writing my Housing-Interface in 3 different languages on 4 different Frameworks, i thought the 5th time would be the charm, and it is.

I remember when i switched to Rails, that i really liked the organization with MVC, it gives you clean space to do your things, Models, Views and Controllers, but one thing was still missing, Performance.

When you have a really large Dataset you need to display, and you want to weed out some of the data, in Ruby frameworks you have three options.

Write your own finder on the Model or the Controller, oooooor you can you do it in the View. I know, i know, you shouldn’t do it in the view, but sometimes you’ve gotta let stuff go for performance. Why?

Imagine this, you have 300 boxes full of Cookies in the back of your store, and you need to restock a whole wall with Chocolate, red sprinkled, weed-laced, etc. Now if you have one employee in the back going through all those boxes and find the broken cookies, and one in the front who takes every cookie, checks the color and puts it on the shelf, you’ve wasted a whole person. Why shouldn’t the guy who stocks the shelves also check if the cookies are fine? He obviously also has to take them one by one and check their color for whatever coloring-scheme the boss wants them to be arranged on the shelf.

This exact same issue is happening with most MVC frameworks, you have your controller or model collecting everything, weeding out, maybe weed out again by some other criteria and then display your data. So what is the difference between Scala Liftweb and Ruby on Rails/Merb?

Scala is fast

Scala is so damn fast, if it were lightning, it would be long gone before anyone knew they were struck.

When i compare stuff like Scala’s .filter or .map to Ruby’s .reject or .collect/.each, i seriously cannot recommend to anyone doing filtering of big datasets in their models or controllers, because it will simply suck the life (and performance) out of your server. If you need to use ruby, do those iterations in the view and “next” if your condition is not met, this has actually sliced the render-times on one particular project in half (5.2s -> 2.4s). What i want to say is, Ruby is fine, but Scala is the bomb!

But why should one write Scala when you could prototype in Ruby, and if it is working for you/your customer/whoever, you can still re-write it if you need it to scale?

That was my initial thought as well, but when you get better with scala, and especially get to know some tweaks and tricks with Liftweb, you can have your Prototype faster and better with Scala than anything else. Even if it is a Prototype, if you spend a few more thoughts on it, it will be ready to simply continue development for the beta or production which will be in fact able to scale. And you should never forget that with Scala, you simply safe so much type-checking code, it’s a breeze compared to everything else.

Liftweb is also fast

Not just in terms of performance, but in terms of development-speed. In my early days, i’ve considered Types to be something people used in the 80s, but the more i encounter bugs related to “oh, it was a string? I thought it was a Float!”, that i got sick and tired of writing tests all day long, so why not save 90% of the tests and write 10% of them simply to check if you functionality is right, not if the returned types are the ones you are looking for.

Lift gives you this really amazing toolset to build web-sites which scale, perform, are secure and use the latest buzzword-technology, as well as save an assload of development-time.

Seriously, i’ve never wooped out a project in an evening, even with Rails or Merb i didn’t get that far, with Liftweb i did that twice in the past 2 months, namely println and CoinMines.

In those past 2 years i’ve tried Scala and Liftweb a couple of times (and of course failed a couple of times), but i somehow caught up on the thought, that web applications do not have to be those loose-code things like they are in Python, Ruby or PHP, and they also don’t need that bureaucratic overhead which Java adds to it, so the Golden middle was clear for me, something where i wouldn’t have to write interfaces, and something which would have Typesafety and scale like hell, therefore i chose Scala and Liftweb.

View First

Well i have to admit, View First sure is an easy concept if you’re coming from PHP, but if you’re used to MVC, that’s really something else. Once you get the hang of it, you will forever be a believer. It just feels sooo right, right like you couldn’t imagine if you haven’t done it.

If you read my twitter, then maybe you’ve heard me writing that before, but Scala and especially Liftweb is an eyeopener and a revelation.

The way you can write concise code which compiles and you can be sure, your float will be a float, unless you’ve opened a Box which wasn’t full, but hey, that’s just gonna happen once or twice before you start writing fault-tolerant code which can handle it. ;)

So if you have some spare time, make sure to check out Scala and Liftweb, i hope it will be a journey and improvement of your work as it was to me.

My gratitude and deep admiration goes out to David Pollak for giving us Liftweb and to Martin Odersky for giving us Scala. Great job!

Liftweb Comet-style updating with CSS-Selectors

Since i have been heavily developing my new project CoinMines, i’ve stumbled upon some nice Comet-possibilites in the Liftweb Google Groups where Timothy Perrett describes his way of doing those replies.

Since this particular part is not documented very well, i will try to shed some light on it.

Example Code

In this particular example, we use the the Clock from the Liftweb Demo and modified the source to do Liftweb-style CSS Selectors.

In order to use that, you need something like this:

What happens is, the NodeSeq we pass into the CometActor is stored in defaultHtml, where you can “reapply” the CSS Selector onto it, getting back the complete NodeSeq but with updated results.

It is really really simple to do amazing things in Lift where other Frameworks just eat up your time.

I am so happy i switched, can’t think of a good reason why i should switch to something else in the near future.

Anyway, there is a Bitcoin, Solidcoin and Namecoin Mining-Pool running on hashrate.tk with my CoinMines Frontend, since it is under heavy development, i suggest you check it out every once in a while, currently doing the statistics.

cheers

Coinmines - a Bitcoin Mining Pool Frontend

Today i finally found some time to do a little work on my project called CoinMines. CoinMines is an Open Source Web-Frontend for PoolServerJ and pushpool. Pooled mining is the process of hashing the transactions that occur in the Bitcoin network. Successfully hashing a block of transactions results in a reward.

Pooled mining is a way for many miners to work together in the process of hashing transactions, and then split the reward among the members based on their contribution. This gives users a steady payout, eliminating some (but not all) of the luck factor.

This means: instead of mining by yourself to get a new block (50 BTC), you can work together with others thus earning Bitcoins in small steps rather than big ones. This also means that with only a few hours of a days work, you can be looking at a 0.2-0.5 BTC a day (give or take depending on your hardware).

So it can handle PoolServerJ and pushpool

Totally! But you have to put PoolServerJ into compatibility-mode.

I’ve taken the time to put configuration-files for both into the configs/ directory of the github repository, so you’ll get started easily with whatever poolserver you’d like to run with. I’d personally recommend PoolServerJ since it’s Java and is highly maintained.

Release 0.1

Since i didn’t like any frontends out there, i took the liberty to write my own frontend which has riped to v0.1 over the past few Months. I am currently working on it, so it should be ready for production in about a few weeks.

Unofficial Mining Pool

Currently there is an unofficial Mining Pool running CoinMines, namely hashrate.tk. The Operator of the Pool is skewb35, but as far as i’ve seen, he keeps up with recent development and is a really nice guy! ;]

If you choose to calculate with hashrate.tk, please keep in mind that you are supporting this project because i get share-statistics to finish up the payment-process, no IP Addresses will be transmitted to me! That’s why i am gonna say this clearly, FUCK OFF GOVT. I no haz ipaddr :<

Btw. if you want to donate, you can send your donations to

  • 1LJBo5gYcymgbaRSYkf8SRxNAUtjzV4wb7.

If you want to setup your own Mining Pool, just visit CoinMines.com for an installation howto.

cheers!

println - a custom made suit

Since i published println, i’ve had a hand full of people asking me, why it is so minimalistic, why i didn’t implement more options and so on. Since most of the people were open to my view of things, i wanted to write a bit about it, so people don’t get the wrong impression.

println does not strive to be complete, nor the feature-richest blogging software. It has a simple task, to be as minimalistic as possible.

Why?

Because i know the problem, that you are looking for a blogging software which can do everything! But when you find it, you often notice after a while, that it has just too much stuff and it gets either slow, complicated or worst case, both.

Having a lot of configuration-options means a lot of code to check for them, this also means a lot of points of error and performance impact on every single “is it enabled?”-check.

I want to give the developer the choice. println is fairly basic, but it is in my opinion a good start for someone who wants to get publishing with something that is more sized to fit.

Think about it: Would you rather present yourself at a job interview in a cheap suit off the shelf or something cut to fit?

Now i know that a lot of people do not care for suits, but at least you won’t take your sloppiest clothes out the closet, but something that fits and looks good.

Enough with the metaphors.

But i don’t know Scala, how can i get started?

Well, Scala is pretty is easy to learn. What would you do when you had to modify some PHP-Guestbook or Forum just so it would fit into your page? Yes you eventually learned PHP and fixed those 3 lines.

What i am saying is, learn Scala if you want to use this particular software. There is no point in people managing software which they can not control, and by control i mean “fix serious shit when the roof is coming down”.

So it is not Wordpress/Serendepity/ubersomething?

NO! But thanks to Java under the Hood, it scales like crazy over Java Application Servers like Jetty or Apache Tomcat.

Also worth mentioning, since it is not PHP, but done with the Liftweb Framework, it almost eliminates the OWASP top 10 security threats. Thanks dpp!

What can you do with it?

You can freely download and use it from github and do whatever you want to the code (BSD-License).

You can also contact my business if you want me to customize println for your needs and help you get started.

Since it is published under a BSD-license, feel free to produce Closed Source Products, but keep the publication-clause in mind!

I hope you get my point and my philosophy, if not, i can’t help you. But in layman’s terms and last metaphore: if you do not know what you’re doing, don’t do it! Or would you change Tires on a Tourbus and bet yours (and other) lifes on it as opposed to calling Triple-A / ADAC?

cheers