Feature

MQTT support coming in LavinMQ 2.1


Written by: Lovisa Johansson


We are happy to share what we have been working on alongside the clustering features released with version 2.0. The upcoming release of LavinMQ 2.1 will include support for MQTT!

MQTT is one of the most widely used messaging protocols for lightweight and low-bandwith communication, and we can’t wait for you to start using it in your projects.

LavinMQ has always been an excellent choice for IoT applications, offering scalability and reliability. With MQTT support, we’re providing even more options for integrating IoT devices, sensors, and mobile applications, giving you greater flexibility to build and manage your messaging solutions.

Whether you work with smart home systems, industrial IoT, or any other connected platform, LavinMQ 2.1 offers the tools to create an efficient and robust communication system.

Complete documentation for MQTT can be found here.

Connecting to LavinMQ with MQTT

To start using MQTT with LavinMQ 2.1, follow these steps:

  1. Send a CONNECT packet to LavinMQ’s MQTT listener on port 1883.
  2. Include the following fields in the CONNECT packet:
    • client_id: A unique identifier for your client.
    • username and password: For authentication.
    • clean_session: Boolean to specify whether to start a clean session.
    • will: An optional field for a Last Will and Testament message.
    • qos: Quality of Service level.

Example using mqtt-client.cr

Here’s a quick example of how you can connect using the mqtt-client.cr library.

require "mqtt-client"
require "mqtt-protocol"

will = MQTT::Client::Message.new("connect", "body".to_slice, 1_u8, false)

client = MQTT::Client.new("localhost", 1883, user: "guest", password: "guest", client_id: "hello", clean_session: false, will: will)

#Do what you want to do with your MQTT-connect
#...

client.disconnect

Stay tuned for more details as we approach the release!