-1

I am using hivemq-mqtt-client-1.3.0.jar and creating a hivemq mqtt5 client. I have specified a keep alive time of 60 secs from client side.

String id = "1";
Mqtt5ClientBuilder builder = Mqtt5Client.builder()
                .identifier(id)
                .serverHost("localhost")
                .serverPort(1883)
                .addConnectedListener(context -> System.out.println("Connected Now"))
                .addDisconnectedListener(context -> System.out.println("Disconnected Connected Now"));
        
        
client = builder.build();
client.toBlocking().connectWith().cleanStart(false).keepAlive(keepAlive).send();
subscribe(id, "1");

Once I connect to hiveMq with the above client, I get a connected log from event.txt file as shown below.

2023-01-03 12:56:39,428 - Client ID: 1, IP: 127.0.0.1, Clean Start: false, Session Expiry: 0 connected.

Now i disconnect my client using below code after 10 secs.

Thread.sleep(10*1000);
client.toBlocking().disconnect();

My expectation is that even though the client disconnected after 10 sec, the broker should keep the connection alive for another 50 sec as keep alive is 60 sec.

But as soon as i disconnect the client, I get disconnected message from broker too, which means keep alive is not working.

2023-01-03 12:56:49,482 - Client ID: 1, IP: 127.0.0.1 disconnected gracefully.

Also I have attached below hivemq broker config.xml file.

<hivemq>
    <listeners>
        <tcp-listener>
            <port>1883</port>
            <bind-address>0.0.0.0</bind-address>
        </tcp-listener>
        <mqtt>
            <keep-alive>
                    <allow-unlimited>false</allow-unlimited>
                    <max-keep-alive>60</max-keep-alive>
            </keep-alive>
        </mqtt>
    </listeners>

    <anonymous-usage-statistics>
        <enabled>true</enabled>
    </anonymous-usage-statistics>
</hivemq>

Please suggest me what I am doing wrong here and any suggestions would be deeply appreciated.

UPDATE******

Now I am going to subscribe to a topic without without sending disconnect message from my client. Pls refer above code again, I have added a function call to subscribe to topic.

public static void subscribe(String id, String cid) {
    try {
        
        client.toAsync().subscribeWith()
                .topicFilter("TEMP/"+id+"/"+cid).callback(publish -> {
                    
                    String message = new String(publish.getPayloadAsBytes(), StandardCharsets.UTF_8);
                    System.out.println("Received message on topic " + publish.getTopic() + ": " + message);

                }).send();
        
        System.out.println("Subscribing to " + "TEMP/"+id+"/"+cid);

Now after this I dont receive any messages on this topic. My keep alive time is 60 secs as shown in conf above, so broker should disconnect my client after 60*1.5 = 90 secs. But even after 90 secs, i dont see my client getting disconnected. Also broker does not send any PINGREQ to client too.

Kindly advice what I should do.

YDev01
  • 1
  • 2

1 Answers1

1

Your understanding is wrong, the broker and client is behaving correctly.

If you call disconnect() then the client will send an explicit DISCONNECT packet to the broker telling it that it has gone. So it will shut everything down.

Keep alive is there for if the network path between the client and broker is broken in the middle, so no packets can flow.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Hi @hardillb, Thank you for your quick response. I have tested the other scenario too, where I dont send disconnect from client , but my broker does not disconnect my client even after keep alive timer expires. Any idea why this might be. I have subscribed to one topic on which no messages are getting published, so ideally after 60 secs, my broker should disconnect client, but its not happening. Pls give your views on the above issue. – YDev01 Jan 03 '23 at 09:42
  • The disconnect will actually be after 1.5 the keepalive value (this is because the broker will send a ping at the keepalive time and then wait for 0.5 keepalive for a response) http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Keep_Alive (Also worth noting that the client will send pings in the background if no other messages are sent) – hardillb Jan 03 '23 at 09:46
  • I have updated the post, kindly pls check. This issue persists even after 1.5 times keep alive expires from broker side – YDev01 Jan 03 '23 at 09:58
  • The client will not send any PINGREQ, the broker will send the PINGREQ and the client will send PINGRESP this is handled in the background by the client, it is NOT something you explicitly code. Please spend some time reading the spec, Stack Overflow is not the right place for an on going discussion about how the protocol works – hardillb Jan 03 '23 at 10:01
  • my bad, kindly ignore the above mistake as this is my 1st post. But yes after spending a lot of time reading the mqtt specs, Broker is not sending any PINGREQ packet to my client. Am I missing any configurations? – YDev01 Jan 03 '23 at 10:06
  • How do you know the broker is not sending the PINGREQ? The broker will be sending that packet if there have been NO other packets sent in keepalive period, the counter gets reset on each MQTT protocol packet sent between the broker and client. – hardillb Jan 03 '23 at 10:08
  • Also this document says that client sends pingreq to broker. So which is right exactly?Am I missing something?https://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over/ – YDev01 Jan 03 '23 at 10:10
  • Also how i know broker is not sending pingreq is because I have subscribed to all topics(#) using another MQTT Client , I dont see any pingreq there – YDev01 Jan 03 '23 at 10:22
  • PINGREQ/PINGRESP are not things you can subscribe to, they are low level protocol commands handled by the client and NOT exposed to the user – hardillb Jan 03 '23 at 10:24
  • ooh that I didnt know, but assuming that pingreq and pingresp are being sent by broker and client, then why does my connection persist even after 10 minutes when keep alive is just 60 seconds? i.e The disconnect listener does not get executed and I dont get "disconnected" message at all. – YDev01 Jan 03 '23 at 10:25
  • Because you've **NOT** been disconnected, the pings will keep the connection alive (as is their intention). As I said at the very start in the answer, you have to break the network between the client and the broker for keepalive to kick in and disconnect the client. – hardillb Jan 03 '23 at 10:33