MQTT is a publish-subscribe messaging protocol commonly used to connect Internet of Things (IoT) applications.
With the release of Diffusion 6.6, Diffusion supports clients connecting with MQTT 5.0. See the 6.6 manual for full details.
A wide range of IoT devices can connect directly to Diffusion, send and receive data, interact with other Diffusion clients, and benefit from Diffusion’s data wrangling features and rich security framework.
MQTT can be used as a lightweight alternative to the Diffusion SDK for simple publish-subscribe use cases, although MQTT connections cannot take advantage of the full range of features available when connecting using the SDK.
Sportsbook is a demo bundled with the default on-premise Diffusion server installation.
Its purpose is to demonstrate Diffusion’s capability as a robust Intelligent Event Data Platform by modeling the kind of data flow a gaming firm would see while providing real-time odds for soccer betting.
To demonstrate the ease of linking an MQTT client with a Diffusion server, we have ported Sportsbook to use pure MQTT instead.
The MQTT Sportsbook demo is installed by default when you install Diffusion 6.6, alongside the Diffusion based Sportsbook demo.
To run it, first download and install Diffusion from here. When installing, make sure “Demos & Examples” is ticked. Once installed, run
diffusion.sh for Linux / Mac, or
diffusion.bat for Windows.
In another terminal, run:
java -jar sportsbook-demo-publisher-jar-with-dependencies.jar under
Fundamentally, the core code of the application remains unchanged; in fact, the only changes revolve around establishing the connection to the Diffusion server.
By ingesting the same
Diffusion SDK Top; MQTT Bottom
Here, the only differences lie in how the path selector is defined, and the definition of a cache object in the MQTT implementation. Whilst Diffusion uses a DSL to define a path selector, MQTT uses regex.
With Diffusion, each time a message is received, it is stored in its CBOR form in a local cache. This value is used to calculate the new value: a delta is taken between the new message value and the cached version. The original Sportsbook UI relied on this to present changes over time to the user.
MQTT does not send the previous value – nor does it keep a local cache like Diffusion, so a manual local cache had to be defined to mimic this behavior. Moreover, this cache retains the whole message, so the speed and bandwidth benefits of taking a delta are lost.
Diffusion SDK Left; MQTT Right
These blocks of code outline how each implementation connects to the Diffusion server. Other than using library specific methods to connect to the server, the general mechanism is the same; connect to the server using the given credentials and define a series of event listeners.
Diffusion SDK Top; MQTT Bottom
These blocks of code deal with handling incoming topic values from the server. As above, while the exact implementations differ due to library differences, the mechanisms are nearly identical. Note that the MQTT implementation has to manually assign a value to
oldValue in order to cache previous values.
We hope this demo shows how easy it is to use MQTT with Diffusion 6.6. You can download and install Diffusion 6.6 now and try it out right away.