HiveMQ

How we can connect MQTT via 443 port

I want to make a connection by using the 443 port. The reason is that I used SSL port 8883. But on some routes, this port blocked. So I want to use secure 443. Can you tell me the example, how I can connect?

Hi @ranaaamer and welcome to the forum :slight_smile:

You can specify the port of the server in the client builder:

  Mqtt5AsyncClient client = MqttClient.builder()
    .useMqttVersion5()
    .serverHost("localhost")
    .serverPort(443); 
    .sslWithDefaultConfig()
    .buildAsync();

  client.connect();

Does this answer your question?

Best regards
Till

Hi @seti thanks for your time. I tried but no luck, here is the code.

MqttClientSslConfig clientSslConfig = MqttClientSslConfig.builder().keyManagerFactory(SSLUtils.getKeyManagerFactoryWithPFX(_context, “”)).trustManagerFactory(SSLUtils.getTrsutManagerFactorWithPFX(_context)).protocols(Collections.singletonList(TlsVersion.TLS_1_2.javaName())).build();
MqttClientTransportConfig mqttClientTransportConfig = MqttClientTransportConfig.builder().serverPort(443).serverHost(AppMode.appRunningMode.getSslUrl()).sslConfig(clientSslConfig).mqttConnectTimeout(5, TimeUnit.SECONDS).build();
MqttClientAutoReconnect autoReconnect = MqttClientAutoReconnect.builder().initialDelay(3, TimeUnit.SECONDS).maxDelay(5, TimeUnit.SECONDS).build();
mqttAndroidClient = Mqtt3Client.builder()
.addConnectedListener(this)
.automaticReconnect(autoReconnect)
.addDisconnectedListener(this)
.identifier(UUID.randomUUID().toString())
.transportConfig(mqttClientTransportConfig) // 8883
.buildAsync();
mqttAndroidClient.connectWith().keepAlive(12).cleanSession(true).send();

Should I add https with serverhost.