📎 Webclip
MQTT for Elixir
The article presents MQTT as an alternative to HTTP for embedded and IoT communication in Elixir. It explains the broker-based pub/sub model, then builds a simple sensor application that publishes temperature readings and listens for interval changes.
Reading notes#
- MQTT is described as a messaging protocol for device communication, using a lightweight binary protocol over TCP/IP and fitting unreliable networks.
- The protocol uses pub/sub, so clients connect to a broker instead of a server.
- A broker such as EMQX can also offer WebSockets, authentication and authorization, streaming to databases, and custom routing rules.
- The sensor example is an ordinary Mix application that can be turned into a Nerves app.
- The sensor module starts an MQTT client, subscribes to
commands/#{clientid}/set_interval, and schedules periodic ticks. - On each tick, it generates a
{Timestamp, Temperature}tuple and publishes it toreports/#{clientid}/temperature. - The dashboard is a Phoenix LiveView app that also acts as an MQTT client.
- It subscribes to
reports/#and publishes interval commands tocommands/#{id}/set_interval. - Incoming temperature reports are converted into a chart rendered with Contex.
- The form on the dashboard sends a new interval to the device topic and updates the charting process state.
- The article notes that retained command messages let a restarted device receive the last configured interval on reconnect.
