MQTT message published from MAUI app to AWS IOT is not showing in MQTT test client

Sreejith Sreenivasan 1,001 Reputation points
2025-04-10T15:40:33.8633333+00:00

I am using below code to publish message from app to AWS IOT:

public async Task PublishMessage(string topic, string payload)
{
    if (_mqttClient == null || !_mqttClient.IsConnected)
    {
        Utility.DebugAndLog("MQTT client is not connected. Cannot publish message.", "");
        return;
    }

    var message = new MqttApplicationMessageBuilder()
        .WithTopic(topic)
        .WithPayload(payload)
        .Build();

    var result = await _mqttClient.PublishAsync(message, CancellationToken.None);
    Utility.DebugAndLog($"Published to topic: {topic}", payload);
    Utility.DebugAndLog("Publish Result:", result?.ReasonCode.ToString());
}

//publishing like below
string message = "{\"groupid\": \"100\", \"lattitude\": \"9.970696\", \"longitude\": \"76.313531\"}";
await PublishMessage("alertbuddies/livelocation/100", message);

I am getting below output after publishing a message:

[0:] Published to topic: alertbuddies/livelocation/100{"groupid": "100", "lattitude": "9.970696", "longitude": "76.313531"}
[0:] Publish Result:Success

I used MQTT test client to verify the published messages are successful or not. For that I subscribed to same topic from MQTT test client, but the message published from app is not showing on the MQTT test client.

User's image

Before publishing from app I have subscribed to the topic and is there anything I missed here? I also tried subscribing to another device to the same topic and published message from one device, and at that time also the published message is not receiving on another device.

.NET Internet of things
.NET Internet of things
.NET: Microsoft Technologies based on the .NET software framework.Internet of things: A concept that aims to extend the benefits of the regular internet, including constant connectivity, remote control ability, and data sharing, to goods in the physical world.
39 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sreejith Sreenivasan 1,001 Reputation points
    2025-05-01T10:01:01.65+00:00

    When the connection is closed by the server when publishing it seems that there is some invalid data or features from mqttv5 used and the server is only 3.1.1. Try setting the protocol version to 3.1.1.

    On 3.1.1, DisconnectedAsync and ApplicationMessageReceivedAsync are not accessible. So use UseDisconnectedHandler and UseApplicationMessageReceivedHandler.

        _mqttClient.UseDisconnectedHandler(OnDisconnected);
        _mqttClient.UseApplicationMessageReceivedHandler(ApplicationMessageReceivedHandler);
    
        
    
    0 comments No comments

  2. Deep Joshi 0 Reputation points
    2025-05-01T12:59:53.56+00:00

    If your MQTT publish shows "Publish Result: Success" but the message isn't appearing in the MQTT test client, here are a few potential causes:


    1. Missing or incorrect AWS IoT policy permissions

    Ensure that the policy attached to your AWS IoT device allows both publishing and subscribing to topics. Without the appropriate permissions, messages may not be delivered to subscribers.


    2. Topic mismatch or incorrect subscription filter

    AWS IoT requires exact matching for topics. Double-check that both the publisher and subscriber are using the exact same topic. The topic names are case-sensitive, so any discrepancy in the topic can cause the issue.


    3. Retain flag not set

    If the message is published before the subscriber connects, and the message isn't marked as retained, the subscriber won't receive it. The retain flag ensures that the last message sent to a topic is delivered to new subscribers upon connection.


    4. Endpoint or credential mismatch

    Make sure both the publishing app and the test client are connecting to the same AWS IoT endpoint and using valid certificates or credentials. Inconsistent configurations between the devices can lead to failed message deliveries.


    5. QoS level mismatch

    Check that both the publisher and subscriber are using the same Quality of Service (QoS) level. A mismatch in QoS could lead to messages being dropped or not received as expected.


    Quick Fix Suggestion: Double-check that your AWS IoT policy is correctly configured, both publisher and subscriber are on the same topic, and ensure your QoS settings match. If the issue persists, make sure the retain flag is set for the messages.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.