2

I am new to the MQTT world and I am trying to create a .Net 5.0 application that connects to a HiveMQ Cloud Broker.

I have created a free broker and I am able to connect to it with HiveMQ Websocket Client.

Here is a screenshot of my host.

enter image description here

I have created MQTT credentials for the host and I am able to connect over the sample client. Here is a screenshot of that client.

enter image description here

This works, I can publish and subscribe to the message queue.

However, now I am trying to translate this to c# and I am not able to connect. I am starting with this example project: https://github.com/rafiulgits/mqtt-client-dotnet-core

Then plugged the values from my cluster instance but I am a getting connection timeout on startup.

Here is what my service configuration looks like:

public static IServiceCollection AddMqttClientHostedService(this IServiceCollection services)
{
    services.AddMqttClientServiceWithConfig(aspOptionBuilder =>
    {
        //var clientSettinigs = AppSettingsProvider.ClientSettings;
        //var brokerHostSettings = AppSettingsProvider.BrokerHostSettings;

        aspOptionBuilder
        .WithCredentials("Test1", "xxxxx") //clientSettinigs.UserName, clientSettinigs.Password)
        .WithClientId("clientId-jqE8uIw6Pp") //clientSettinigs.Id)
        .WithTcpServer("xxxxxxxxxxxxxx.s2.eu.hivemq.cloud", 8884); //brokerHostSettings.Host, brokerHostSettings.Port);
    });
    return services;
}

private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttClientOptionBuilder> configure)
{
    services.AddSingleton<IMqttClientOptions>(serviceProvider =>
    {
        var optionBuilder = new AspCoreMqttClientOptionBuilder(serviceProvider);
        configure(optionBuilder);
        return optionBuilder.Build();
    });
    services.AddSingleton<MqttClientService>();
    services.AddSingleton<IHostedService>(serviceProvider =>
    {
        return serviceProvider.GetService<MqttClientService>();
    });
    services.AddSingleton<MqttClientServiceProvider>(serviceProvider =>
    {
        var mqttClientService = serviceProvider.GetService<MqttClientService>();
        var mqttClientServiceProvider = new MqttClientServiceProvider(mqttClientService);
        return mqttClientServiceProvider;
    });
    return services;
}

I am not sure where I am going wrong, any help would be greatly appreciated.

A. Hasemeyer
  • 1,452
  • 1
  • 18
  • 47

2 Answers2

2

You appear to be trying to connect to the WebSocket endpoint (port 8884) in your code, when I suspect you really should be using the normal TLS endpoint (port 8883)

Also you will need to use different clientid values if you want to have both clients connected at the same time as having matching will mean the clients will continuously kick each other off the broker.

(edit: on looking closer the client ids are actually different, but only in the last char)

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Thanks for the response, when I switched it to use TLS and set the certificates to AllowUntrustedCertificates = true, IgnoreCertificateChainErrors = true, IgnoreCertificateRevocationErrors = true it connected successfully. I assume this is only safe for testing purposes and I must install actual certificates when I go to deploy. – A. Hasemeyer Dec 16 '21 at 18:00
  • If you need to server certificate you can create it with the OpenSSL s_client. openssl s_client -connect :8883 -showcerts < /dev/null 2> /dev/null | sed -n '/BEGIN/,/END/p' > server.pem This will create a file called “server.pem”, which you can use as “Server Certiciate”. – fraschbi Dec 17 '21 at 14:02
0

I had this issue in two days ago and it seems coming form TLS confgurations/settings. By the way, my Startup.cs service injections and some configurations were same with yours. I have .NetCore app and I am trying to connect my own hivemq broker (cloud side).

In this case we need to add additional option to our mqtt client option build phase.

When I add this code, Auth problems gone.

.WithTls();

Here is part of the client option codes should like that

AddMqttClientServiceWithConfig(services,optionBuilder =>
        {
            var clientSettings = BrokerAppSettingsProvider.BrokerClientSettings;
            var brokerHostSettings = BrokerAppSettingsProvider.BrokerHostSettings;

            optionBuilder
            .WithCredentials(clientSettings.UserName, clientSettings.Password)
            .WithTls()                
            .WithTcpServer(brokerHostSettings.Host, brokerHostSettings.Port);
        });
        return services;

We can consider this as a different solution.