Akka/Scala together with Wowza Media Server

So my first Open-Source-Community-Christmas-Present (or OSCCP) is for the Akka and Wowza-Community.

Yesterday i found a nice way to use Scala inside the Wowza Media Server. It’s a Streaming-Server like Red5, but it works better. It can handle Live-Streams, Pre-recorded Content and all those kinds of things, haven’t yet had the chance read the full docs, but as far as my buddy elm0 told me, this is the way to go in regards to flash video crapshoes.

So what does it do?

The Wowza Media Server (as already said) is an alternative to Red5. It is capable of coders writing extensions/callback-Handlers in java, and drop those .jars into the lib-folder of it.

This is in my opinion a very nice way of letting coders decide what the backend should allow/reject, and it also allows for a much more fine grained way to connect multiple of those servers to a “Master”/Coordination-Node and keep each user-session managed.

Lucky for us Scala folks, we can simply drop our own jars in there, and even better, we can use Akka to build a Remote-Connection to our Master- or Frontend-Server.

Since today i don’t feel like much copy and pasting, (plus i wanna have some more fun with Wowza and Akka), i’ll simply redirect you to the code. ;)

May the source be with you

The project can be found on GitHub, i also removed the advertisement clause from the BSD-license for this project, old habits prevail much too often.

Thanks

To my very best friend elm0 who got me into Wowza last night, and also thanks to mikukopteff who’s Github Project Streamlet inspired me to do this.

Happy Christmas

Imagine...

You have a successful company, employ a handfull of people, make enough money to not have to worry about where your food is coming from and how you’re going to pay your rent. Yes, it sounds nice.

But then suddenly your grandma dies, your beloved grandma no one ever could replace. Imagine a warm and loving grandma who loves to cook all day and take long walks with her dog. So that one person you could always turn to who would always have something meaningful AND helpful to say is gone. But she lived half way around the world, so what do you do?

Since money is not an issue, you feel your duty calling and book the next trip to get to your grandmother’s funeral without hesitation. Imagine your best friend saying “hey dude, i’ve taken 2 weeks vacation, you wanna go to hawaii?”, wouldn’t that be awesome?

So a few months pass by, and you decide to take a trip to spain to attend your buddy’s birthday party. While you’re having the time of your life there, you meet that one person. That one person in 7 billion people! Possibly you see parts of your grandmother in her, her attitude towards life, her cooking, what she likes to eat and drink. But she lives 300km away, but okay, you should be able to manage that, you think. You’re positive! Energized!

When you get back home, you catch on the tv-ticker that your biggest client just went bankrupt, and that you probably gonna have to close your shop. This is bad news! But you pull through the whole ordeal, put a lot of money in it so you don’t have to declare Chapter 11, dust yourself off and start fresh!

So you reset your life, start a whole new company and forget the past, looking at the future with your new “girlfriend” ahead. Imagine yourself dreaming of that person all day, drifting away, asking your self what she might be doing, but i guess you know how that feels.

Imagine yourself being apart from the person you love, being forbidden by that person to move there, not knowing what’s happening in that persons life. What would you do?

You get angry, with everything, with everyone except that one person, even more than when you closed your company, even worse than when you’ve had to let all your employees go, and for the worst of all, it makes you even more mad than when your grandma died. Imagine being so angry, that you are unable to think clearly, being in rage all day every day, is this worth living?

But you find the strength, day after day, month after month, year after year, to stay that one person whom you might see once every other month, faithful. Imagine you having to work all day every day, since you cannot enjoy free time. Imagine forcing yourself to work relentlessly despite the psychological pain that is in you. Spare time is being considered punishment since your thoughts only drift around that one in 7 billion, so you work, day in and day out. Your sleep cycle gets fucked up, you don’t eat anymore, or at least barely. And again i ask you, what would you do?

After doing this for over two years, you simply cannot deal with it anymore, you break down. You don’t know what to do, where to go to make it better. Still, being unable to get to the one person that you love the most, whom you would give the world if you had it, it drives you bad crap crazy.

At last, you’ve suffocated that one person with your need for basic human interaction, so you will get your keys to your apartment back, which have been used once a year, you have christmas presents for that one person laying around which you hoped to gift personally (rather than DHL), because you didn’t know if you were going to see that person before christmas again or not.

When you find that one person that connects you to the world, you become someone different, someone better. When that person is taken from you, what do you become then?

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