Home > Software design >  Google cloud run unable to connect MQTT
Google cloud run unable to connect MQTT

Time:01-26

Currently I am developing a .Net6 application with a some controllers and a minimal MQTT Server. So far I have everything working correctly on my local machine, however, when deploying to Google Cloud Run (GCR) I am unable to connect to the MQTT Server.

I noticed that the GCR container wants you to map incoming traffic to a single port (8080 in my case), however I am running MQTT on port 1883 (default) and unable to connect to it. The controllers running on port 8080 are accessible and work fine.

I need some direction toward a solution, preferably in a single container.

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.UseKestrel(o =>
{
    o.ListenAnyIP(1883, l => l.UseMqtt()); 
    o.ListenAnyIP(8080);
});

...

var app = builder.Build();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints
        .MapConnectionHandler<MqttConnectionHandler>("/mqtt",
            httpConnectionDispatcherOptions =>
                httpConnectionDispatcherOptions.WebSockets.SubProtocolSelector = protocolList => protocolList.FirstOrDefault() ?? string.Empty);
});

app.UseMqttServer(server => server.StartAsync());
app.MapControllers();
app.Run();

CodePudding user response:

A possible option is to not use 2 ports.

If you use MQTT over WebSockets then your broker can share a port with the HTTP server.

  • Related