B4R Question MQTT port 1883 blocked on mobile networks (4G/)? Is this expected and what is the best workaround?

Cesar_Morisco

Active Member
Licensed User
Longtime User
Hello everyone,
I’m working on an IoT project using ESP8266 + B4R, and everything works perfectly on a normal Wi-Fi network.
However, when I switch to a mobile network (4G/LTE), the device cannot connect to the MQTT broker using port 1883. The connection either fails or drops immediately.
I’ve read that many mobile operators block port 1883 for security reasons.
My questions are:
Is this really common / expected behavior on mobile networks?
Is the problem related to the ESP/B4R or is it purely a network/operator restriction?
What is the recommended solution for MQTT over mobile networks?
MQTT over TLS (port 8883)?
MQTT over WebSocket (port 443)?
Or switching to HTTPS instead of MQTT?
I’d appreciate hearing real-world experiences and best practices, especially from B4R users.
Thanks in advance.
 

aminoacid

Well-Known Member
Licensed User
Longtime User
Hello everyone,
I’m working on an IoT project using ESP8266 + B4R, and everything works perfectly on a normal Wi-Fi network.
However, when I switch to a mobile network (4G/LTE), the device cannot connect to the MQTT broker using port 1883. The connection either fails or drops immediately.
I’ve read that many mobile operators block port 1883 for security reasons.
My questions are:
Is this really common / expected behavior on mobile networks?
Is the problem related to the ESP/B4R or is it purely a network/operator restriction?
What is the recommended solution for MQTT over mobile networks?
MQTT over TLS (port 8883)?
MQTT over WebSocket (port 443)?
Or switching to HTTPS instead of MQTT?
I’d appreciate hearing real-world experiences and best practices, especially from B4R users.
Thanks in advance.

I use MQTT a lot in both Mobile and Desktop applications and I have never encountered an issue where the carrier blocks any OUTBOUND port. Inbound ports are blocked for obvious reasons and some carriers use private NAtted IP addresses but since we connect to the MQTT broker outbound, this should not restrict the connection in any way. In addition, my apps are used by hundreds of users on mobile devices with a variety or cellular providers (in the US) and none have reported any issues. Things may be different in other countries but this is not the case in the US at least.

To make sure that it is really blocked, try running TELNET on your mobile device and connect to 1883. See if it connects.

What MQTT Broker are you using? Let me know which one and I can try it out on my mobile device. This applies if you are doing this in the US.

Yes, Try the MQTT TLS Port 8883 and any other port that is exposed by the Broker using TELNET to see if it is open.

I don't use B4R but this should apply to all Apps.
 
Upvote 0

aminoacid

Well-Known Member
Licensed User
Longtime User
Here is the definitive test you can use. Download one of my Apps that use MQTT on Port 1883

Android:

IOS:

If it works and you see data, then the port is not blocked. If it does not you have confirmed that the carrier is blocking the port.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
I’m working on an IoT project using ESP8266 + B4R, and everything works perfectly on a normal Wi-Fi network.
Is your server published in internet or is a local server at home?
If it's the second case, you should open your router port and redirect it to your local server.
What url are you using to access to your server?
 
Upvote 0

aminoacid

Well-Known Member
Licensed User
Longtime User
Is your server published in internet or is a local server at home?
If it's the second case, you should open your router port and redirect it to your local server.
What url are you using to access to your server?

I agree. Are you connecting to an external or internal MQTT Server? What is the address that you are connecting to? You have not provided any details.
 
Upvote 0

Cesar_Morisco

Active Member
Licensed User
Longtime User
Hello!


Thanks for your question.
I have always used MQTT in the way shown in the following link:
👉 https://www.b4x.com/android/forum/threads/send-mqtt-data-receive-app.149592/


It used to work fine for me, both with an internal MQTT server and an external one.
However, for some time now, it stopped working when I’m using mobile data (4G/5G) on my phone — even though I haven’t changed the working code.


When I’m connected to the same Wi-Fi network as the MQTT broker or an internal network, everything works as expected.
The issue appears only when using the mobile network; the broker refuses the connection / doesn’t respond.


I didn’t change libraries or my working code — this behavior started happening out of the blue some time ago.


If you want to see the full thread with details, here is the link:
👉 https://www.b4x.com/android/forum/threads/send-mqtt-data-receive-app.149592/


Regarding your question about whether I’m connecting to an external or internal MQTT server — it worked with both, but the problem clearly shows up when using mobile data.


Best regards!
b4a:
Private MQTT As MqttClient
    Private MQTTUser As String = "teste"
    Private MQTTPassword As String = "00000000"
    Private MQTTServerURI As String = "tcp://broker.hivemq.com:1883"'change for other broker

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
'Connect to CloudMQTT broker
Sub MQTT_Connect
    MQTT_Nome=Nome_Text3.Text
    MQTT_Senha=Senha_Text4.Text
    MQTT_Url=Url_Text1.Text
    MQTT_Port=Port_Text2.Text
    Dim ClientId As String = Rnd(0, 999999999) 'create a unique id
    MQTT. Initialize("mqtt",MQTT_Url & ":" & MQTT_Port , ClientId)
    Dim ConnOpt As MqttConnectOptions
    ConnOpt.Initialize(MQTT_Nome,MQTT_Senha)
    MQTT.Connect2(ConnOpt)
End Sub

'MQTT CONNECT AND SUBSCRIBE TO TOPIC
Sub MQTT_Connected (Success As Boolean)
    If Success = False Then
        Log(LastException)
        xui.MsgboxAsync("Error de Conexão","")
    Else
        Conectar.Text = "Conectado"
        Conectar.TextColor = xui.Color_Red
        MQTT.Subscribe("Control_Rele8/#", 1)'change the name of the topic!
        MQTT.Subscribe("Relay_0",1)'change the name of the topic!
        MQTT.Subscribe("Relay_1",1)'change the name of the topic!
        MQTT.Subscribe("Relay_2",1)'change the name of the topic!
        MQTT.Subscribe("Relay_3",1)'change the name of the topic!
        MQTT.Subscribe("Relay_4",1)'change the name of the topic!
        MQTT.Subscribe("Relay_5",1)'change the name of the topic!
        MQTT.Subscribe("Relay_6",1)'change the name of the topic!
        MQTT.Subscribe("Relay_7",1)'change the name of the topic!
        MQTT.Subscribe("Relay_8",1)'change the name of the topic!
        MQTT.Subscribe("Temp_Ds",1)'<--- ADD
    End If
End Sub

Private Sub Conectar_Click
    If Conectar.Text = "Conectado" Then
        xui.MsgboxAsync("Você está Conectado!","Casa Controle 1")
    Else
        MQTT_Connect
    End If
End Sub


Sub mqtt_MessageArrived(Topic As String, payload()As Byte)
    If Conectar.Text = "Conectado" Then
    Temp = BytesToString(payload,0,payload.Length,"UTF-8")
        If Topic = "Temp_Ds" Then
            Label4.Text = Temp & " °C"
    End If
    End If
    End Sub
    


Private Sub Desconectar_Click
    If MQTT.IsInitialized Then
        MQTT.Close
    End If
    ExitApplication
End Sub
 
Upvote 0
Top