Learning from activator templates

There is a growing list of reactive applications based on iteratees that have been packaged and deployed as activator templates. At the time of writing this book, we have identified more than five templates and you can look at a few of them. They often mix technologies such as WebSockets with Akka for communication and message processing and, on the client side, JavaScript frameworks such as Angular.js, to often provide simple HTML rendering.

As the Typesafe activator templates HTML page lets you select tags to filter out projects depending on some keywords, you may check the appropriate projects by selecting the reactive checkbox.

Reactive stocks

This sample is a project based on the Java version of Play. It graphically demonstrates the real-time update of stock values (that are randomly generated for simplicity). It contains both Java and Scala code. An Akka StockActor actor is instantiated for every stock symbol, and its role is to maintain a list of all users watching this stock. Some additional functionality queries a twitter API to retrieve all tweets matching a particular symbol (for example, http://twitter-search-proxy.herokuapp.com/search/tweets?q=appl). This knowledge can then be processed to calculate a sentiment index that should help to decide whether to buy this stock or not. The following screenshot illustrates graphically what the app looks like, once it is run:

Reactive stocks

Reactive real-time search

To demonstrate some integration between ElasticSearch and the reactive features of the Typesafe stack through Play iteratees and Akka, this sample exhibits how to push log events to a browser. As a reminder, ElasticSearch (http://www.elasticsearch.org) is a distributed real-time search and an analytics engine based on the well-established Apache Lucene (https://lucene.apache.org) full-text search engine.

It notably provides a percolation feature, that notifies your application when new content matches your search criteria (instead of having to poll the search engine to check regularly for new updates).

To emulate content, an Akka LogEntryProducerActor actor is responsible for generating random log entries each time it receives a Tick message. These messages are produced at regular intervals by a MainSearchActor actor that also acts as a coordinator for the search. Finally, an ElasticSearchActor actor implements the percolation feature by interacting with an embedded ElasticSearch server (EmbeddedESServer) that is started from the Play Global class. Instead of pushing information to the browser via WebSockets, the sample uses Server Side Events (SSE) as it only needs one-way communication once the search criteria are known.

Further information on the template and all the code behind it is available at https://github.com/DrewEaster/realtime-search. In particular, the query syntax to be entered for a search is defined as Lucene syntax and is specified at http://lucene.apache.org/core/4_3_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package_description.

If we execute this sample by installing and running the activator template (with the > activator run command from the root of the template project), we can open a browser at localhost:9000 and enter GET as the search criterion. After a few seconds, some browser output should progressively be displayed, as shown in the following screenshot:

Reactive real-time search

The Play-Akka-Angular-WebSocket template

As another example of reactively pushing information to the browser, this sample updates a clock on the client by scheduling an actor. The role of this actor is to send events in the JSON format via a WebSocket connection by using the Play WebSocket.async[JsValue] method call. The Angular.js JavaScript framework is used on the client side and the GUI looks like the following screenshot once it starts running:

The Play-Akka-Angular-WebSocket template

The number of activator templates that illustrate reactive applications is growing. Also, you can check out from time to time the new templates that are available, without upgrading the version of the activator each time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset