What is covered?

- Flow
- AJAX Connector and JavaScript Client to communication to a browser
- JSON bindings
- The @Schedule annotation

Description

Demonstrates how to use AJAX to communicate from a Mule Service to the browser.

Under the Hood

At the server, a single component called CityStroller is responsible for generating random GPS coordinates and publishing them. It uses the previously published coordinates and generates a random value for distance and angle. This service method has a @Schedule annotation that will cause the method to be invoked every 3 seconds; a cron expression could also be used. The CityStroller component produces GPSCoord objects. GPSCoord is a JavaBean that will get automatically serialized to JSON so that it can be easily consumed by JavaScript in the browser.

Inside the Flow

The Ajax connector allows Mule to bind services and flows to ajax channels on the browser. We declare our CityStroller component, it is declared a singleton since we maintain state between requests (the current coordinates). Finally, we define an outbound ajax channel called '/gpswalker'. The GPSCoord data produced by CityStroller will be sent over this channel to any listeners that have subscribed. Remember, Mule will automatically serialise the GPSCoord to JSON.

In the browser we use the new Mule JavaScript client to receive coordinates from the server.