The bundle includes a small bootstrap script that
can start Activator. To start Typesafe Activator's UI:

In your File Explorer, navigate into the directory that the template was extracted to, right-click on the file named "activator.bat", then select "Open", and if prompted with a warning, click to continue:

Or from a command line:

C:\Users\typesafe\play-akka-angular-websocket> activator ui

This will start Typesafe Activator and open this template in your browser.

Option 3: Create a play-akka-angular-websocket project from the command line

If you have Typesafe Activator, use its command line mode
to create a new project from this template.
Type activator new PROJECTNAME play-akka-angular-websocket on the command line.

Option 4: View the template source

Option 5: Preview the tutorial below

We've included the text of this template's tutorial below,
but it may work better if you view it inside Activator
on your computer. Activator tutorials are often designed
to be interactive.

Preview the tutorial

Quick Overview

This project shows how to use Play2, Akka and AngularJS for create a simple WebSocket based Timer application.

The basic idea is to create a sort of roundtrip from the user action to the response, using ajax calls for all the http requests and a single WebSocket for all the responses. This is just an example, in a real world application we need some more hacks to handle an high users number, and of course we need to write tests.

The Idea

When the user open the application, a WebSocket is associated with the userid, in that case,because this is an example, if user doesn't have an userId in session it is generated automatically and saved in session. (In Play the session is just a cookie, and there are some helpers to manage it)

When user interact with the application clicking on one of the buttons, through Angular an ajax call is sended to the server. The ajax call is handled by a Play Action, this Action instead of send the result of the computation directly to te client, send a message to the TimerActor and close straightaway the http request with an Ok response. TimerActor handle the message, run its logic and send the operation response to the user through the associated WebSocket

This approach has two advantages :

in case of long computation, we don't need to keep opened the http connection

if the user has more then one window opened, all windows receive the same data, and maintain the same state.

Play Controller

The Play controller has two main functionalities, the first is to create the WebSocket, and the second is to send the right message to the actor when an ajax call come in

To create the socket, inside the withAuthWS method we call the function WebSocket.async[JsValue] () which take a Future[(Iteratee[JsValue, Unit], Enumerator[JsValue])]. This is generated from :

withAuth provide a basic authentication, and then the function send a message to the actor and send the Ok("") response to the client and close the http connection

The Actor

The actor manage the creation of channels and the one to one relation between channels and users, and manage also the timer scheduler, increase the timer for active users and send the updated data to users through the sockets, writing inside the channels.

case StartSocket(userId) => When the message StartSocket come in, the actor get or create the channel for the current user

The main method here is
Concurrent.broadcast[JsValue] which create a tuple
(Enumerator[JsValue], Channel[JsValue]). This tuple is composed by an enumerator and the related channel, when we'll write inside the channel through
channel.push() method, the message is enumerated from the enumerator. So the as last operation for the message StartSocket, the actor send back to the sender, the AppController in that case, this enumerator which is user in the controller for create the socket

The second interesting part in the actor is case UpdateTime() => The message UpdateTime is sended every second to the actor thanks the Akka helper context.system.scheduler.schedule(0 second, 1 second, self, UpdateTime()) When this message come in, the actor update the time for every user in the list usersTimes, and write in the channel the current time webSockets.get(userId).get.channel push Json.toJson(json) and this message will be sended to the client within the socket.

Pretty trivial are the Start and Stop cases, the Stop case after removed the user from the usersTimes list, send also a message to the client with timer 0, for reset the timer label.

From the blog

While Reactive application development is off to a roaring start and becoming mainstream, this leads to demands on Operations that are simply not met by yesterday’s software architectures and technologies. The pressure facing enterprises to manage resilient, responsive systems is brutal, yet most existing technologies available today are not designed to deploy and manage Reactive systems running on clusters. It’s due to this fact that Operations face a higher risk of downtime by using inappropriate tools/practices at a time when being unavailable is more costly than ever. So why is this happening? Well, it's not 2005 anymore–and why that's a problem for Operations is explained here...

Ten months ago we posted about architectural changes to Typesafe Activator. After a few a lot of yak shaves, side projects, and detours, we have Activator 1.3.0 based on sbt server, a new setup where multiple clients can share the same instance of sbt. sbt server is also available in ABI-stable protocol-stable form for other clients (IDEs, command line, desktop status icon, whatever you can think of) to try out.