Appkata-mqtt
Run your own MQTT server for event-based messaging on Fly Read this guide on Fly.Rationale
Your modern application is more reactive than ever and with an event-based messaging platform like MQTT, you can communicate and react more than ever. MQTT allows for publish and subscribe messages to be sent to topics and broadcast to listening clients with options to persist data and enforce quality of service on connections. We’ll be using Mosquitto, a popular free MQTT broker, to create a persistent messaging hub on Fly.Preparing
You will need Mosquitto installed locally as we’ll be using some of its tools in the process of creating and testing our server. The other thing we’ll need is mkcert, which allows you to create your own certificate authority and certificates. If you are in macOS, install Homebrew and then runbrew install mosquitto mkcert.
Initialising
We’re ready to begin building on the official Mosquitto image, eclipse-mosquitto and making some changes to it with a Dockerfile:/etc/mosquitto. We then copy over a configuration file, passwords, and certificates to the image. We wrap up with a startup command which runs Mosquitto using our configuration.
Now we need to create a slot for the application on Fly. Run
appkata-mqtt (though you’ll have to find another name because fly app names are unique - if you are having trouble making one miss out the app name and Fly will generate one for you). It will also create a fly.toml file which will use the Dockerfile to build the app. There are to the most important things to know are the app name and the region where the app will deploy - in this case, lhr.
All that’s missing now are all those files. Let’s go fill in the gaps.
mosquitto.conf
This file sets up the server:/mosquitto/data/mosquitto.db.
Then we are on to password configuration: we require them and the password file will be in /etc/mosquitto/passwd. Third, we set up the TLS/SSL parameters. Mostly the locations of files we will create soon, and some settings, like require_certificate set to false to turn off client authentication and a reasonable selection of TLS version for communications.
Passwords
Let’s create that password file. This is simply a matter of coming up with a username and password and then runningCertificates
This is where we usemkcert. In one command, we’ll ask it to generate
appkata-mqtt.fly.dev - remember to replace the hostname with your app name followed by .fly.dev to make your own certificates.
As mkcert generates its own CA root, to use these certificates, we need a copy of that root.
Services and Volumes
With the Mosquitto configuration done, we now need to prepare the Fly configuration. The first part of this is creating the volume to store the persistent data. We’ve already mentioned the directory name inmosquitto.conf and the persistence location setting. Now we need to create a Fly disk volume to hold that data in the region where the app will deploy. We got that information when we initialized the app at the start. We’ll make a mosquitto_data volume.
fly.toml file. Open it and add these lines:
/mosquitto/data. Before you close, there is something else to do in fly.toml and thats configure the services. By default, the fly.toml file directs traffic on port 80 and port 443 to the internal port like so:
Deploying and Testing
To deploy our server is simple:Discuss
- You can discuss this example on its dedicated community.fly.io topic.