Receiving MQTT Messages with Quarkus (Part 1)

Andreas Eberle
arconsis
Published in
3 min readMar 3, 2023

In this series of posts, we will have a look on how to integrate your Quarkus service with MQTT. In this first post, we will learn how to receive messages from MQTT. Check out the second post to learn about sending MQTT messages with Quarkus.

Receiving MQTT Messages with Quarkus

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for efficient communication between devices in constrained environments, such as those with limited bandwidth, processing power, and battery life. This is why MQTT is widely used in the Internet of Things (IoT) and other applications that require efficient and reliable messaging between devices.

Video Tutorial

If you prefer a video tutorial instead of the text, have a look at our YouTube channel where we also cover the Quarkus MQTT integration.

Receiving MQTT Messages with Quarkus YouTube Tutorial

Project Setup

If you want to setup a new project with Quarkus to connect to an MQTT, we recommend to go to code.quarkus.io. On this link, we directly added the needed extensions for you and you only need to download the bundle. Alternatively, check out this tag in the accompanying repo.

To learn more about code.quarkus.io, you can have a look at my previous post explaining everything you need to know.

When you have an existing project already, you’ll need to add the extension for smallrye reactive messaging with MQTT.

implementation("io.quarkus:quarkus-smallrye-reactive-messaging-mqtt")

Public MQTT Test Broker

In this blog series, we’ll use a public MQTT broker for testing the integration. This makes it easy to send test messages to our Quarkus and view them in a browser without having to set up our own queue. For this, we’ll use mqtthq.com.

Connecting to MQTT

First, we need to configure the connection properties to the broker in the Quarkus application.properties by adding the following lines:

mp.messaging.incoming.arconsis-twitch.connector=smallrye-mqtt
mp.messaging.incoming.arconsis-twitch.host=public.mqtthq.com
mp.messaging.incoming.arconsis-twitch.port=1883
mp.messaging.incoming.arconsis-twitch.topic=arconsis/twitch/#
  • With this, we define an incoming connection. This means we can use this connection to consume messages coming from the MQTT broker to our Quarkus.
  • In the example above, arconsis-twitch is a freely choosable name of how you want to call this connection inside your Quarkus. We will later use this name in the @Incoming annotation to reference this connection.
  • The first line defines the type of connector we want to use for the microprofile messaging connector. The Quarkus extension smallrye-reactive-messaging supports multiple connectors (e.g. also for Kafka, AMQP, …). In our case, we want to use the smallrye-mqtt connector.
  • Host and port are used to connect to the broker.
  • The topic is used to identify which messages we want to listen to. This can include the pattern symbols + (wildcard for a single path segment) and # (wildcard for everything under the given prefix).
  • There are more properties e.g. for authentication. You can check them out here.

Consuming MQTT Messages with Quarkus

To consume the messages in our code, we simply need to add a new function annotated with @Incoming("arconsis-twitch") where arconsis-twitch is the name of the incoming connector we specified in the application.properties. The code can look like this (Kotlin):

@ApplicationScoped
class SimpleMqttReceiver {

private val logger = Logger.getLogger(SimpleMqttReceiver::class.java.name)

@Incoming("arconsis-twitch")
fun receiveMeasurement(byteArray: ByteArray) {
val messageString = String(byteArray)
logger.info { "Received raw message: $messageString" }
}
}

And that’s it! When we now go to our test broker and send a message to a topic we are listening to (e.g. arconsis/twitch/hello-world), we see the message being received by Quarkus! The log looks like this:

2023-02-27 17:27:50,964 INFO  [io.ver.mqt.imp.MqttClientImpl] (vert.x-eventloop-thread-0) Connection with public.mqtthq.com:1883 established successfully
2023-02-27 17:27:51,336 INFO [io.sma.rea.mes.mqtt] (vert.x-eventloop-thread-0) Subscription outcome: Future{result=0}
2023-02-27 17:27:54,144 INFO [com.arc.twi.mqt.rec.SimpleMqttReceiver] (vert.x-eventloop-thread-0) Received raw message: Hello World!
2023-02-27 17:29:10,796 INFO [com.arc.twi.mqt.rec.SimpleMqttReceiver] (vert.x-eventloop-thread-0) Received raw message: Hello MQTT World!

You can check out the code in our GitHub repository under the tag simple-message-receiver.

To learn more about Quarkus and other software engineering topics, make sure to follow arconsis on medium. E.g. here is a great article comparing the runtime performance of Quarkus with that of Spring Boot.

--

--

Andreas Eberle
arconsis

Solutions Architect & Software Engineer @arconsis