Unboxing: Play2

Overview
Owain Lewis looks at the benefits offered by one of the newer web frameworks, Play2, which uses the Scala programming language.
Author
Owain Lewis
Date

About Unboxing

Unboxing is our regular feature where someone at Box UK spends a few hours playing with a new framework, library, tool or technique and blogs about his or her experience. You can read more Unboxing features here.

What is Play2?

Play2 is a modern web framework that runs on the JVM (Java Virtual Machine) and allows developers to write fast, scalable applications in either Scala or Java, offering benefits for businesses interested in building quick and reliable web solutions. I’ve been using it on and off for the last few months and wanted to give a quick overview of the framework.

Perhaps this phrase from the Play Framework website sums it up best:

“Play Framework provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.”

Play2 already has some pretty serious real-world use from forward-thinking companies like LinkedIn, Klout, the BBC and the Guardian.

Getting to grips

So why should you choose Play2 over one of the numerous older web frameworks out there? Learning a new framework is a significant investment so it needs to offer some clear advantages over existing solutions. Here are some of the things I like about it so far:

Scala

There’s no doubt that this is what originally drew me to Play2. Scala is a powerful functional programming language that runs on the JVM. Although Play started as a Java-centric web framework it has been completely rewritten in version 2 and Scala is now the main development language. Scala combines many elements of advanced functional programming languages like Haskell, Clojure and OCaml with first-class support for OOP, making it highly practical and an absolute joy to use. The downside to a language like this, though, is the huge learning factor for developers new to functional programming (with features such as monoids, lenses, pattern matching, monads, higher order functions, immutability etc.). For a large team this could be a potential issue as most people don’t have much exposure to this style of programming and a new programming paradigm is something that requires substantial time to learn.

Type safety

The Scala type system is extremely powerful (think Haskell rather than Java or C#) and the compiler catches an enormous number of potential errors in your code. I found myself feeling more sure of my Scala code than I would be in a dynamic language like Python or Ruby, where you’d need a lot of extra unit tests to catch even the most basic of errors. In my opinion, this leads to better design and more reliable software.

JVM

By running on the JVM, Play2 gives developers the option to code in Java or Scala as well as access to all the libraries from the Java ecosystem. This is a big advantage if you’re already familiar with these libraries.

Going deeper

Here we’ll take a deeper look at some of the more interesting Play2 features:

Concurrency

Play2 has extensive support for async programming, and Scala libraries like Akka give developers access to Erlang-style actors along with a host of other concurrency abstractions like futures and promises. This kind of thing is becoming more and more important for modern web applications.

Websockets

Another modern aspect to the framework is Play’s support for HTML5 websockets. I’ve not seen this kind of thing in a framework before and it was a pleasant surprise.

What does it look like?

Play2 code looks pretty much the same as any other MVC stack out there with controllers, models and views. Controllers in Play2 should look familiar to most developers:

The Play2 templating language out of the box took me by surprise; it was easy to use and as flexible as anything else I’ve used.

Database Support

Out of the box Play2 comes with Anorm, but the new Typesafe library Slick seems to be fairly popular as well. This is one area where I felt the learning curve was pretty steep as Slick integration isn’t very well-documented at present, meaning that you’re more or less left to figure things out from blog posts around the web.

Testing

The Scala testing ecosystem is really strong and Play2 also gives you some examples of how to get started. Given that Play2 runs on the JVM you can use any Java library or the newer Scala ones. Here is an example using the built-in Specs2 framework:

Final thoughts

Everything about Play2 feels slick and modern. The development team have clearly taken inspiration from other frameworks and created something that is really impressive. Support for async HTTP, a strong type system via Scala, access to the JVM, websockets and traditional MVC design give you pretty much everything you’d want from a modern web framework.

Overall I’d say that Play2 with Scala is one of the nicest web stacks I’ve ever used. It combines the functional programming power and type safety of Scala, feels familiar enough to developers coming from other MVC frameworks and offers a host of useful libraries for building modern web applications.

My only criticism would be that the developer community (at least here in the UK) feels relatively small and finding people with extensive Scala/FP experience is pretty difficult. Also the framework, being fairly new, feels like it’s still in a state of growth meaning at the moment things probably aren’t as stable as the more mature web frameworks out there. Another thing that might irk developers coming from more dynamic languages is the type system. Even Java and .NET developers may find it strange at first as the Scala type system is a complex beast (covariant and contra-variant types are fairly daunting) which has a lot in common with Haskell and ML.

Documentation is pretty good but there isn’t the wealth of community knowledge and plugins out there if you get stuck. For developers new to the framework I recommend looking at some of the sample applications that come with Play2.

The Scala learning curve is very steep for developers without any prior experience in functional programming languages like Haskell or ML. That said, it’s very easy to get productive in a relatively short time and the effort is well worth it.

Want to know more?

I really hope Play2 continues to grow in popularity over the next few years; it’s a great web framework with a bright future. If you want to learn more, head over to the Play Framework website: 

http://www.playframework.com/

Comments

Add a comment

  • 03 Jun 2013 12:31 Box UK Staff

    True, the dev community is small in the UK but it’s definitely growing – there’s a South West Scala group just started up at http://www.meetup.com/South-West-Scala

  • 03 Jun 2013 12:35

    I have not had a chance to play with Scala yet, but looking at the samples within your blog post it seems very ‘clean’.

    I think I will need to take a closer look at this one. Thanks for the post!

  • 03 Jun 2013 12:40

    Yeah it’s a great framework and language. Worth checking out the Odersky book which gives a pretty good introduction.

    There’s also a new book from Manning giving an introduction to the framework.

    http://www.manning.com/hilton/

Add your comment

If provided, we will link to this from your name