Unable to publish a message from MAUI iOS version

Sreejith Sreenivasan 1,001 Reputation points
2025-04-28T15:39:53.9+00:00

I have opened a bug last week regarding the issue with publishing a message from MAUI app here. The suggested solution was to use the 3.1.1 version and it is working fine for Android platform.

But in iOS not able to connect to MQTT broker with 3.1.1 version . So I used 4.3.7.1207 in iOS and connection and subscription are working fine, but when try to publish a message the connection is getting disconnected.

Below code I am using to publish a message:

string message = "{\"type\": \"publish\", \"message\": \"Help needed!\", \"userid\": \"101\"}";
await PublishMessage("myappname/publish/100", message);

// Create a message to publish
var mqttMessage = new MqttApplicationMessageBuilder()
    .WithTopic(topic)
    .WithPayload(message)
    .Build();

// Publish the message to the topic
await _mqttClient.PublishAsync(mqttMessage, CancellationToken.None);

When try to publish like above, the MQTT connection is getting disconnected and reconnecting. Also the publishing message is not success. To connect MQTT broker I am using IAM user credentials with websocket.

I have tried all versions from 3.1.1 but no luck.

I have posted a detailed question on StackOverFlow.

I am stuck on this issue more than a week, please suggest a solution as soon as possible. I have already opened a bug in GitHub, but no response. So if anyone knows a solution please suggest it.

.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
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sreejith Sreenivasan 1,001 Reputation points
    2025-05-01T09:59:52.6533333+00:00

    I was calling the connect function from OnStart() method of App.xaml.cs like below:

    protected override async void OnStart()
    {
    	try
    	{
    		base.OnStart();
    		await MqttService.Instance.ConnectAsync();
    	}
    	catch (Exception exception)
    	{
    		Utility.SendCrashReport(exception);
    	}
    }
    

    The issue here is related to calling an async method (ConnectAsync) from OnStart, which does not support await properly in .NET MAUI.

    App.OnStart() is not truly asynchronous — it's a void method, and calling await inside it doesn't work reliably.

    This can cause exceptions to be swallowed silently or result in undefined behavior like the method never being hit.

    I have resolved this issue by calling the connect function from home page constructor.

    0 comments No comments

Your answer