Android Question ConnectAndReconnect - Mqtt.close problem

amorosik

Expert
Licensed User
App Android with mqtt connection to Mosquitto on pc
I use the code like ConnectAndReconnect Erel example
The question is: how to close connection faster than 30 sec?

I tried Erel's suggestion on this post (change mqtt client_id) but without success
(on row 'mqtt_client.Connect2(mo)' there an error 'Connection not close')

The final question is: how can I quickly reactivate a connection when the broker is back up and running?

B4X:
Sub ConnectAndReconnect
    Do While flag_connetti_mqtt
   
        If mqtt_client.IsInitialized Then mqtt_client.Close
'        Do While mqtt_client.IsInitialized
'            Sleep(100)
'            Loop
       
        mqtt_client_id="MyApp_" & Rnd(0, 999999)
        mqtt_client.Initialize("EventMqtt", mqtt_uri, mqtt_client_id)
       
        mqtt_username="nome_utente_mqtt"
        mqtt_password="password_mqtt"
       
        Dim mo As MqttConnectOptions
        mo.Initialize(mqtt_username, mqtt_password)
        mo.SetLastWill("smartphone/sconnesso" , serializator.ConvertObjectToBytes( mqtt_client_id), 0, False)
        mqtt_client.Connect2(mo)
       
        Wait For EventMqtt_Connected (Success As Boolean)
       
        If Success Then
            mqtt_connected=True
            Dim stringa_connected As String= "COM=MQTT_CONNESSO;"
           
            Try
                mqtt_client.Subscribe("Cons2MyApp",0)
                mqtt_client.Publish2("MyApp2console", serializator.ConvertObjectToBytes(stringa_connected), 0, False)
                Catch
                Log(LastException)
                End Try

            Do While flag_connetti_mqtt And mqtt_client.Connected
                Log ("Ping verso consolle")
               
                Try
                    Catch
                    Log(LastException)
                    End Try
               
                Sleep(1000)
                Loop
               
            mqtt_connected=False
            Else
            mqtt_connected=False
            End If
           
        Loop
End Sub
 
Solution
jMQTT v1.30 is attached. Please try it. I haven't tested it myself.

You can configure the two timeouts:
MqttClient.QUIESCE_TIMEOUT = 5000 'from 30 seconds to 5seconds.
MqttClient.DISCONNECT_TIMEOUT = 5000 'from 10 seconds to 5 seconds. Timeout for the disconnect packet.

teddybear

Well-Known Member
Licensed User
I tried Erel's suggestion on this post (change mqtt client_id) but without success
(on row 'mqtt_client.Connect2(mo)' there an error 'Connection not close')
You omitted to define the local variable mqtt_client.
B4X:
        mqtt_client_id="MyApp_" & Rnd(0, 999999)
        Dim mqtt_client As MqttClient '<====='
        mqtt_client.Initialize("EventMqtt", mqtt_uri, mqtt_client_id)

        mqtt_username="nome_utente_mqtt"
        mqtt_password="password_mqtt"
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
jMQTT v1.30 is attached. Please try it. I haven't tested it myself.

You can configure the two timeouts:
MqttClient.QUIESCE_TIMEOUT = 5000 'from 30 seconds to 5seconds.
MqttClient.DISCONNECT_TIMEOUT = 5000 'from 10 seconds to 5 seconds. Timeout for the disconnect packet.
 
Last edited:
Upvote 0
Solution

teddybear

Well-Known Member
Licensed User
jMQTT v1.30 is attached. Please try it. I haven't tested it myself.

You can configure the two timeouts:
MqttClient.QUIESCE_TIMEOUT = 5000 'from 30 seconds to 5seconds.
MqttClient.DISCONNECT_TIMEOUT = 5000 'from 10 seconds to 5 seconds. Timeout for the disconnect packet.
It works
 
Upvote 0

amorosik

Expert
Licensed User
You omitted to define the local variable mqtt_client.
B4X:
        mqtt_client_id="MyApp_" & Rnd(0, 999999)
        Dim mqtt_client As MqttClient '<====='
        mqtt_client.Initialize("EventMqtt", mqtt_uri, mqtt_client_id)

        mqtt_username="nome_utente_mqtt"
        mqtt_password="password_mqtt"

Yes, you are right
Having defined it as a variable in Process_Globals, I thought that would be enough

Sub Process_Globals
Private mqtt_client As MqttClient
Private mqtt_client_id As String
End Sub

Then I tried inserting the definition as you suggested, and it worked
So Erel's original suggestion to start a new client works perfectly
 
Upvote 0

amorosik

Expert
Licensed User
jMQTT v1.30 is attached. Please try it. I haven't tested it myself.

You can configure the two timeouts:
MqttClient.QUIESCE_TIMEOUT = 5000 'from 30 seconds to 5seconds.
MqttClient.DISCONNECT_TIMEOUT = 5000 'from 10 seconds to 5 seconds. Timeout for the disconnect packet.

Thank you very much, tested, it works
This is THE solution
 
Upvote 0
Top