Chapter 2. Your first reactive web application

 

This chapter covers

  • Creating a new Play project
  • Streaming data from a remote server and broadcasting it to clients
  • Dealing with failure

In the previous chapter, we talked about the key benefits of adopting a reactive approach to web application design and operation, and you saw that the Play Framework is a good technology for this. Now it’s time to get your hands dirty and build a reactive web application. We’ll build a simple application that connects to the Twitter API to retrieve a stream of tweets and send them to clients using WebSockets.

2.1. Creating and running a new project

An easy way to start a new Play project is to use the Lightbend Activator, which is a thin wrapper around Scala’s sbt build tool that provides templates for creating new projects. The following instructions assume that you have the Activator installed on your computer. If you don’t, appendix A provides detailed instructions for installing it.

Let’s get started by creating a new project called “twitter-stream” in the workspace directory, using the play-scala-v24 template:

~/workspace » activator new twitter-stream play-scala-2.4

This will start the process of creating a new project with Activator, using the template as a scaffold:

2.2. Connecting to Twitter’s streaming API

2.3. Streaming tweets to clients using a WebSocket

2.4. Making the application resilient and scaling out

2.5. Summary