mosca icon indicating copy to clipboard operation
mosca copied to clipboard

Reconnecting Issue

Open uronly14me opened this issue 11 years ago • 17 comments

Hello,

I used mosca in production and I got some issues. Client developer said that server closed the connection. And mqtt of the client reconnecting many times per second.

Below is iOS log.

2015-04-06 15:57:42.874 Project[50429:3837628] self.topic=cc/+
2015-04-06 15:57:43.386 Project[50429:3837557] client is connected with id cc
2015-04-06 15:57:43.387 Project[50429:3837557] self.topic=cc/+
2015-04-06 15:57:43.388 Project[50429:3837628] MQTT is disconnected: 7
2015-04-06 15:57:43.653 Project[50429:3837557] subscribed to topic cc/+
2015-04-06 15:57:43.914 Project[50429:3837628] client is connected with id cc
2015-04-06 15:57:43.915 Project[50429:3837628] self.topic=cc/+
2015-04-06 15:57:44.460 Project[50429:3837557] client is connected with id cc
2015-04-06 15:57:44.461 Project[50429:3837557] self.topic=cc/+
2015-04-06 15:57:44.462 Project[50429:3837628] MQTT is disconnected: 7
2015-04-06 15:57:44.713 Project[50429:3837557] subscribed to topic cc/+
2015-04-06 15:57:44.958 Project[50429:3837628] client is connected with id cc
2015-04-06 15:57:44.959 Project[50429:3837628] self.topic=cc/+
2015-04-06 15:57:50.591 Project[50429:3837628] MQTT is disconnected: 7
2015-04-06 15:57:50.617 Project[50429:3837557] client is connected with id cc
2015-04-06 15:57:50.618 Project[50429:3837557] self.topic=cc/+
2015-04-06 15:57:50.866 Project[50429:3837557] subscribed to topic cc/+
2015-04-06 15:57:51.169 Project[50429:3837628] client is connected with id cc

connect -> subscribe -> connection closed is repeated. Can you tell if it is the problem from server or client?

uronly14me avatar Apr 06 '15 11:04 uronly14me

It is impossible to tell, there can be multiple reasons.

mcollina avatar Apr 06 '15 14:04 mcollina

I think server has no reason to disconnect the client. Do you know if there is settings to limit the count of clients? Front-end developer said when he connect to mosquitto.org, it works alright.

uronly14me avatar Apr 06 '15 14:04 uronly14me

It's really hard to debug your code without having access to it. Disconnection might happen for all reasons, from misconfiguration to an actual bug. However, without a way to reproduce the problem, it is impossible for me to help you. One thing: only one client with a given clientId is accepted at the same time, so older connections will be severed. Il giorno lun 6 apr 2015 alle 16:33 Sangbeom Han [email protected] ha scritto:

I think server has no reason to disconnect the client. Do you know if there is settings to limit the count of clients? Front-end developer said when he connect to mosquitto.org, it works alright.

— Reply to this email directly or view it on GitHub https://github.com/mcollina/mosca/issues/234#issuecomment-90082093.

mcollina avatar Apr 06 '15 16:04 mcollina

i call this "reconnection storm", in my case is for unstable network, i use non standard solution, i count reconnections, if so many reconnections in 5 minutes, client must be wait for 10 minutes and reconnect again. so simple and work fine.

sometimes mobile networks store and forward packets, i find over 60 seconds of delay and client do not receive the connack in this case set more time to connection timeout.

in paho client:

MqttConnectOptions x = new MqttConnectOptions(); x.setKeepAliveInterval(60); x.setConnectionTimeout(60); x.setCleanSession(false);

mazhack avatar Apr 06 '15 16:04 mazhack

and more important, after #229 and #230 patch, reconnections are less, try it...

mazhack avatar Apr 06 '15 16:04 mazhack

Thank both of you! I will try it.

uronly14me avatar Apr 07 '15 14:04 uronly14me

And I found that #181 is also quite similar to this issue. I also have the memory leakage. When memory utilization is full, it stops being connected.

uronly14me avatar Apr 07 '15 14:04 uronly14me

I used Paho MQTT library to connect to Mosca, and this 'reconnecion storm' could be reproduced every single time, almost instantly after I load the following test page. I have Mosca 0.30.0 running on a Uuntu server inside LAN, and my test script is as follows:

<html>
    <head>
        <meta charset="utf-8">
        <title>WebSockets msg test</title>
        <script src="./mqttws31.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var host = '192.168.1.115', 
                port = 63019, 
                random = Math.random().toString().replace(/\./ig, ''), 
                client_id = 'test-' + random, 
                topic = 'test-topic', 
                qos = 1;
            var client = new Paho.MQTT.Client(host, port, client_id);
            client.onMessageArraived = function(msg){
                console.info('Received msg: ' + (new Date(msg.time)));
            };
            var onSuccess = function(resp){
                client.subscribe(
                    topic, 
                    {
                        qos: qos, 
                        onSuccess: function(resp){
                            console.log('Subscribed ' + topic);
                        }, 
                        onFailure: function(resp){
                            console.log('Subscription of ' + topic + ' failed');
                        }
                    }
                );
                var msg = new Paho.MQTT.Message(JSON.stringify({time: new Date() / 1.0}));
                message.destinationName = topic;
                message.qos = qos;
                console.log(msg);
                client.send(msg);
            };
            client.onConnectionLost = function(){
                client.connect({
                    timeout: 10, 
                    mqttVersion: 3, 
                    cleanSession: false, 
                    keepAliveInterval: 60, 
                    onSuccess: onSuccess, 
                    onFailure: function(r){
                        console.error(r, 'Conenction failed');
                    }
                });
            };
            client.connect({
                timeout: 10, 
                mqttVersion: 3, 
                cleanSession: false, 
                keepAliveInterval: 60, 
                onSuccess: onSuccess, 
                onFailure: function(r){
                    console.error(r, 'Conenction failed');
                }
            });
        </script>
    </body>
</html>

I tested the above page in Chrome 43.0.2357.130 m (64-bit), and from the console I get nothing. But there are lots of messages printed by Mosca, and I can see the reconnection is happening very fast. Part of the messages printed by Mosca are as follows:

{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"topic":"test-topic","qos":1,"msg":"subscribed to topic","time":"2015-06-26T08:25:56.658Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"client connected","time":"2015-06-26T08:25:56.659Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"closed","time":"2015-06-26T08:25:56.769Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"topic":"test-topic","qos":1,"msg":"subscribed to topic","time":"2015-06-26T08:25:56.778Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"client connected","time":"2015-06-26T08:25:56.778Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"closed","time":"2015-06-26T08:25:56.883Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"topic":"test-topic","qos":1,"msg":"subscribed to topic","time":"2015-06-26T08:25:56.887Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"client connected","time":"2015-06-26T08:25:56.888Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"closed","time":"2015-06-26T08:25:57.006Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"topic":"test-topic","qos":1,"msg":"subscribed to topic","time":"2015-06-26T08:25:57.007Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"client connected","time":"2015-06-26T08:25:57.008Z","v":0}
{"name":"mosca","hostname":"5b3fc281102c","pid":11,"client":"test-036645159707404673","level":30,"msg":"closed","time":"2015-06-26T08:25:57.151Z","v":0}

This is only happening with Paho MQTT library. I've tested MQTT.js and it works as expected.

JokerQyou avatar Jun 26 '15 08:06 JokerQyou

And also, if I press Ctrl - C to end Mosca during this 'reconnection storm', there's actually a WebSockets error printed to the console of Chrome, which indicates the client has not yet finished handshake phase. see the screenshot below. image

Forgot to say that my Mosca is running without persistence and backend.

JokerQyou avatar Jun 26 '15 08:06 JokerQyou

Has that Mosca instance any configuration? Il giorno ven 26 giu 2015 alle 10:41 Joker Qyou [email protected] ha scritto:

And also, if I press Ctrl - C to end Mosca during this 'reconnection storm', there's actually a WebSockets error printed to the console of Chrome, which indicates the client has not yet finished handshake phase. see the screenshot below. [image: image] https://cloud.githubusercontent.com/assets/1465267/8373758/20203786-1c22-11e5-87f7-eb43ecb4d762.png

— Reply to this email directly or view it on GitHub https://github.com/mcollina/mosca/issues/234#issuecomment-115577387.

mcollina avatar Jun 26 '15 08:06 mcollina

I see on mobile data, that my client is disconnected/connected right before it keepalive arrives in it's ping cycle. Does any of you experiencing this on Redis !?

behrad avatar Jun 26 '15 09:06 behrad

Sorry to disturb you but I found some typos in my code, I'll test a few times more and see if it's the problem of myself. Is a persistence required for Mosca if I want to use QoS 1?

JokerQyou avatar Jun 26 '15 09:06 JokerQyou

Is a persistence required for Mosca if I want to use QoS 1?

yes.

behrad avatar Jun 26 '15 09:06 behrad

Thanks. It was the problem with my code itself. I was going to report both the 'reconnection storm' and a strange situation in which Mosca is sending previous message repeatedly to connected clients. I'll double check to make sure it's not a false positive issue.

JokerQyou avatar Jun 26 '15 09:06 JokerQyou

I've managed to solve all my problems last weekend.

If you found Mosca sending duplicate messages at a very high rate, I suggest checking both client and server code. I've had this problem for like a month or two, which was caused by QoS mismatching on two sides. I used to use mosquitto as MQTT broker and used QoS 2, and after changing broker to Mosca, I forgot to change the client side QoS to 1, so the broker is always waiting for the last response to make sure the message was received, but there is no such response in QoS 1.

Hope this is useful for someone who is experiencing a duplicate messages problem.

JokerQyou avatar Jun 30 '15 06:06 JokerQyou

then we all are fine if this issue is closed now?

behrad avatar Jun 30 '15 06:06 behrad

:dancers:

mcollina avatar Jun 30 '15 07:06 mcollina