In loklak server, messages are collected from various sources and indexed using Elasticsearch. To know when a message of interest arrives, users can poll the search endpoint. But this method would require a lot of HTTP requests, most of them being redundant. Also, if a user would like to collect messages for a particular topic, he would need to make a lot of requests over a period of time to get enough data.
For GSoC 2017, my proposal was to introduce stream API in the loklak server so that we could save ourselves from making too many requests and also add many use cases.
Mosquitto is Eclipse’s project which acts as a message broker for the popular MQTT protocol. MQTT, based on the pub-sub model, is a lightweight and IOT friendly protocol. In this blog post, I will discuss the basic setup of Mosquitto in the loklak server.
Installation and Dependency for Mosquitto
The installation process of Mosquitto is very simple. For Ubuntu, it is available from the pre installed PPAs –
Once the message broker is up and running, we can use the clients to connect to it and publish/subscribe to channels. To add MQTT client as a project dependency, we can introduce following line in Gradle dependencies file –
After this, we can use the client libraries in the server code base.
The MQTTPublisher Class
The MQTTPublisher class in loklak would provide an interface to perform basic operations in MQTT. The implementation uses AsyncClientListener to connect to Mosquitto broker –
The publish method for the class can be used by other components of the project to publish messages on the desired channel –
We also have methods which allow publishing of multiple messages to multiple channels in order to increase the functionality of the class.
Starting Publisher with Server
The flags which signal using of streaming service in loklak are located in conf/config.properties. These configurations are referred while initializing the Data Access Object and an MQTTPublisher is created if needed –
The mqttPublisher can now be used by other components of loklak to publish messages to the channel they want.
Adding Mosquitto to Kubernetes
Since loklak has also a nice Kubernetes setup, it was very simple to introduce a new deployment for Mosquitto to it.
Changes in Dockerfile
The Dockerfile for master deployment has to be modified to discover Mosquitto broker in the Kubernetes cluster. For this purpose, corresponding flags in config.properties have to be changed to ensure that things work fine –
The Mosquitto broker would be available at mosquitto.mqtt:1883 because of the service that is created for it (explained in later section).
The Docker image used in Kubernetes deployment of Mosquitto is taken from toke/docker-kubernetes. Two ports are exposed for the cluster but no volumes are needed –
Exposing Mosquitto to the Cluster
Now that we have the deployment running, we need to expose the required ports to the cluster so that other components may use it. The port 9001 appears as port 80 for the service and 1883 is also exposed –
After creating the service using this configuration, we will be able to connect our clients to Mosquitto at address mosquitto.mqtt:1883.
In this blog post, I discussed the process of adding Mosquitto to the loklak server project. This is the first step towards introducing the stream API for messages collected in loklak.
- Deploying loklak Server on Kubernetes with External Elasticsearch – http://blog.fossasia.org/deploying-loklak-server-on-kubernetes-with-external-elasticsearch/.
- Mosquitto Documentation – https://mosquitto.org/documentation/.
- Manage data in containers – https://docs.docker.com/engine/tutorials/dockervolumes/.
- Services in Kubernetes – https://kubernetes.io/docs/concepts/services-networking/service/.
- xenqtt API Documentation – http://xenqtt.sourceforge.net/apidocs/0.9.6/.