/hivemq-kotlin-extension-example

Kotlin version of the HiveMQ Hello World extension

Primary LanguageKotlinApache License 2.0Apache-2.0

HiveMQ 4 Kotlin Hello World Extension

Type: Demonstration Extension

Version: 4.3.1

License: Apache License Version 2.0

Purpose

This is the Kotlin version of the orginal HiveMQ Hello World extension. Its purpose is to demonstrate how to write HiveMQ extensions using Kotlin instead of Java.

The Hello World extension sets a ClientLifecycleEventListener which logs the MQTT Version and identifier of every connecting and disconnecting client and registers a PublishInboundInterceptor to modify the payload of every Publish with the topic 'hello/world' to 'Hello World!'.

There is a Maven Archetype available to generate a basic extension from the IDE in Java.

It is strongly recommended to read the HiveMQ Extension Documentation to grasp the core concepts of HiveMQ extension development.

Using Kotlin with Maven

You can find information on how to us Kotlin with Apache Maven in official Kotlin documentation.

Installation

  1. Clone this repository into a Java 11 maven project.

  2. Run mvn package goal from Maven to build the extension.

  3. Move the file: "target/hivemq-hello-world-extension-4.3.1-distribution.zip" to the directory: "HIVEMQ_HOME/extensions"

  4. Unzip the file.

  5. Start HiveMQ.

First Steps

Manual Testing

Connect with an MQTT client of your choice. You should see a log message with its identifier and MQTT version.

Automatic Testing

Execute the HelloWorldInterceptorIT for automatic testing of the extension. It uses the HiveMQ Testcontainer to automatically package, deploy and run the extension inside a HiveMQ docker container. The test creates a HiveMQ MQTT Client to publish and receive a message with the topic 'hello/world'. It checks whether the payload has been changed correctly to 'Hello World!'.

Next steps

Awesome, you got your first HiveMQ 4 Kotlin extension working.

Now read the HiveMQ Extension Documentation to see what extensions can do.

Need help?

If you encounter any problems, I am happy to help.