MCPcopy Index your code
hub / github.com/ballerina-platform/module-ballerina-mqtt

github.com/ballerina-platform/module-ballerina-mqtt @v1.4.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.4.1 ↗ · + Follow
161 symbols 367 edges 26 files 25 documented · 16%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Ballerina MQTT Library

Build codecov Trivy GraalVM Check GitHub Last Commit

This Library provides an implementation to interact with MQTT servers via MQTT client and listener.

MQTT is a lightweight, publish-subscribe, machine to machine network protocol for message queue/message queuing service.

Publisher and subscriber

MQTT publisher

A MQTT publisher is a MQTT client that publishes messages to the MQTT server. When working with a MQTT client, the first thing to do is to initialize the client. For the publisher to work successfully, an active MQTT server should be available.

The code snippet given below initializes a publisher client with the basic configuration.

import ballerina/mqtt;
import ballerina/uuid;

mqtt:ClientConfiguration clientConfiguration = {
    connectionConfig: {
        username: "ballerina",
        password: "ballerinamqtt"
    }
};

mqtt:Client mqttClient = check new (mqtt:DEFAULT_URL, uuid:createType1AsString(), clientConfiguration); // A unique id needs to be provided as the client id

Using the publish api of this client, messages can be sent to the MQTT server.

check mqttClient->publish("mqtt/test", {payload: "This is Ballerina MQTT client!!!".toBytes()});

MQTT subscriber

A MQTT subscriber is a client responsible for reading messages from one or more topics in the server. When working with a MQTT subscriber, the first thing to do is initialize the subscriber. For the subscriber to work successfully, an active MQTT server should be available.

The code snippet given below initializes a subscriber with the basic configuration.

mqtt:ListenerConfiguration listenerConfiguration = {
    connectionConfig: {
        username: "ballerina",
        password: "ballerinamqtt"
    },
    manualAcks: false   // When set to false, the MQTT acknowledgements are not sent automatically by the subscriber
};

mqtt:Listener mqttSubscriber = check new (mqtt:DEFAULT_URL, uuid:createType1AsString(), "mqtt/test", listenerConfiguration);

This subscriber can be used in the mqtt:Service to listen to messages in mqtt/test topic.

service on mqttSubscriber {
    remote function onMessage(mqtt:Message message, mqtt:Caller caller) returns error? {
        log:printInfo(check string:fromBytes(message.payload));
        check caller->complete();
    }

    remote function onError(mqtt:Error err) returns error? {
        log:printError("Error occured ", err);
    }
}

The mqtt:Caller can be used to indicate that the application has completed processing the message by using complete() api.

Core symbols most depended-on inside this repo

Shape

Method 136
Class 23
Enum 2

Languages

Java100%

Modules by API surface

compiler-plugin-tests/src/test/java/io/ballerina/stdlib/mqtt/compiler/MqttServiceValidationTest.java28 symbols
compiler-plugin/src/main/java/io/ballerina/stdlib/mqtt/compiler/MqttFunctionValidator.java16 symbols
native/src/main/java/io/ballerina/stdlib/mqtt/listener/MqttListenerCallbackImpl.java14 symbols
native/src/main/java/io/ballerina/stdlib/mqtt/client/ClientActions.java13 symbols
native/src/main/java/io/ballerina/stdlib/mqtt/utils/MqttUtils.java11 symbols
native/src/main/java/io/ballerina/stdlib/mqtt/listener/ListenerActions.java8 symbols
native/src/main/java/io/ballerina/stdlib/mqtt/client/MqttClientCallbackImpl.java8 symbols
compiler-plugin/src/main/java/io/ballerina/stdlib/mqtt/compiler/PluginUtils.java7 symbols
compiler-plugin/src/main/java/io/ballerina/stdlib/mqtt/compiler/PluginConstants.java6 symbols
compiler-plugin-tests/src/test/java/io/ballerina/stdlib/mqtt/compiler/CodeSnippetGenerationCodeActionTest.java6 symbols
compiler-plugin/src/main/java/io/ballerina/stdlib/mqtt/compiler/MqttServiceAnalysisTask.java5 symbols
compiler-plugin/src/main/java/io/ballerina/stdlib/mqtt/compiler/MqttCodeTemplateWithoutCallerParameter.java5 symbols

For agents

$ claude mcp add module-ballerina-mqtt \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page