For one of our client, we need to show a map with vehicles position updated in real-time.
So I began to make a prototype using Play! framework, with its latest released version 2.0, using the Java API. I started from the websocket-chat of the Play! 2.0 samples.
The purpose of the prototype is to show a moving vehicle on a map. The location of the vehicle is sent to the server through a REST call (at the end, it will be sent by an Android app), and the connected users can see the vehicle moving in real-time on their map.
First, let’s look at a small demo !
The interesting part is the one using the WebSockets. On the client side, it is quite standard :
When the client receive a JSON data from the websocket, it moves the marker on the map. And if an error occurs on the websocket (the server is stopped for instance), a pretty error is displayed thanks to Twitter Bootstrap :
On the server part, the websocket is created by the Application controller, and is handled by the MapAnime.java Akka actor; it accesses Akka native libraries to deal with the events from the controller.
The “register” and “moveTo” methods are called by the controller, they send messages to the Akka system. These messages are processed by the “onReceive” method. For instance, when it receives a MoveMessage, it creates a JSON object with the longitude and the latitude and it is sent to the clients through the websockets.
I also quickly wrote a test class which parse a text file, and send REST requests with a new location to the server every 100ms.
The project is hosted on Github. It works with Google Chrome v17 and Firefox v11.
To test it,
- download Play! 2.0
- clone the Git repo
- start “play run” in the project directory
- connect to “http://localhost:9000/map“
- in another terminal, run “play test” to send REST requests and to make the vehicle move 🙂
The problem I need to solve now is that the application is not Stateless, because in the Actor, I store a Map of connected clients. Maybe I’ll need to look at Redis or something, any help would be greatly appreciated.
So in conclusion, I was able to quickly develop a working prototype, and I think I’ll try to use Play! 2.0 in several projects 😉
- Highly productive
- Typesafe view templates based on Scala
- LessCss integration
- Akka integration
- No need to learn Scala for the moment, hooray ! 😉
To be improved:
- The Scala compile times should be increased, because on my PC, it takes up to 4s to compile a view, and it breaks my flow (I use the “~run” command to gain 1s when switching from my IDE to my web browser)
- The Scala compiler errors are cryptic
- I cannot deploy the demo on Heroku because it does not support (yet ?) websockets
Update 2: I was successfully able to deploy the app on a cloud platform which allows websockets, dotCloud, by using this method. The result was (sorry!) available here : http://maptracker-ndeverge.dotcloud.com/mapGoogle+