ottopaulsen / MMM-MQTT

MQTT Client module for MagicMirror2
MIT License
57 stars 29 forks source link

Multiple duplicate server connections #57

Closed ZoneMR closed 2 years ago

ZoneMR commented 2 years ago

Every time the MMM browser client refreshes, multiple new connections are established to the mqtt server.

The first time the client loads, in response to MQTT_CONFIG, a single connection is made.

The second time, two new connections are made, in addition to the existing connection.

The third time, three new connections are made, in addition to the existing three.

I have my magic mirror set to auto-refresh every two hours, and the exponential opening of new connections causes the mqtt server to run out of available sockets and shit itself.

See log below, this what happens on the third client connection after startup:

0|run  | [05.10.2022 14:14.15.811] [LOG]   MMM-MQTT: Starting client for 192.168.68.200:1883 user undefined
0|run  | [05.10.2022 14:14.15.811] [LOG]   MMM-MQTT: Connecting to mqtt://192.168.68.200:1883
0|run  | [05.10.2022 14:14.15.812] [LOG]   MMM-MQTT: Starting client for 192.168.68.200:1883 user undefined
0|run  | [05.10.2022 14:14.15.812] [LOG]   MMM-MQTT: Connecting to mqtt://192.168.68.200:1883
0|run  | [05.10.2022 14:14.15.813] [LOG]   MMM-MQTT: Starting client for 192.168.68.200:1883 user undefined
0|run  | [05.10.2022 14:14.15.813] [LOG]   MMM-MQTT: Connecting to mqtt://192.168.68.200:1883
0|run  | [05.10.2022 14:14.15.815] [LOG]   MMM-MQTT connected to mqtt://192.168.68.200:1883
0|run  | [05.10.2022 14:14.15.815] [LOG]   MMM-MQTT: subscribing to tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR
0|run  | [05.10.2022 14:14.15.815] [LOG]   MMM-MQTT connected to mqtt://192.168.68.200:1883
0|run  | [05.10.2022 14:14.15.815] [LOG]   MMM-MQTT: subscribing to tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR
0|run  | [05.10.2022 14:14.15.816] [LOG]   MMM-MQTT connected to mqtt://192.168.68.200:1883
0|run  | [05.10.2022 14:14.15.817] [LOG]   MMM-MQTT: subscribing to tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR,tele/garage/consumer-unit/SENSOR

Not explored in detail, but looks like there's some issue with deduplication in addServers and/or an issue where startClients doesn't consider client connections which have already been made.

ottopaulsen commented 2 years ago

I have never seen this before. Can you post your config here? (Remember to hide any secrets)

ZoneMR commented 2 years ago

It should be easy to replicate, as the issue happens whenever the MMM browser client is refreshed.

I don't believe my config has anything particularly unusual in it?

        {
            module: 'MMM-MQTT',
            position: 'bottom_left',
            header: 'Electricity',
            config: {
                logging: false,
                useWildcards: false,
                mqttServers: [
                {
                    address: '192.168.68.200',
                    port: '1883',
                    subscriptions: [
                        {
                            topic: 'tele/garage/consumer-unit/SENSOR',
                            jsonpointer: '/ENERGY/Power',
                            label: 'Power',
                            suffix: 'kW',
                            divide: 1000,
                            decimals: 2,
                            maxAgeSeconds: 60,
                            colors: [
                                { upTo: 0.5, value: "blue" },
                                { upTo: 1, value: "#4080ff" },
                                { upTo: 2, value: "" },
                                { upTo: 5, value: "orange" },
                                { upTo: 1000, value: "red" },
                            ],
                        },
                        {
                            topic: 'tele/garage/consumer-unit/SENSOR',
                            jsonpointer: '/ENERGY/Today',
                            label: 'Today',
                            suffix: 'kWh',
                            decimals: 2,
                            maxAgeSeconds: 60,
                            colors: [
                                { upTo: 20, value: "" },
                                { upTo: 30, value: "orange" },
                                { upTo: 1000, value: "red" },
                            ],
                        },
                        {
                            topic: 'tele/garage/consumer-unit/SENSOR',
                            jsonpointer: '/ENERGY/Voltage',
                            label: 'Voltage',
                            suffix: 'V',
                            decimals: 0,
                            maxAgeSeconds: 60,
                            colors: [
                                { upTo: 220, value: "blue" },
                                { upTo: 250, value: "" },
                                { upTo: 500, value: "red" },
                            ],
                        },
                        {
                            topic: 'tele/garage/consumer-unit/SENSOR',
                            jsonpointer: '/ENERGY/Current',
                            label: 'Current',
                            suffix: 'A',
                            decimals: 1,
                            maxAgeSeconds: 60,
                            colors: [
                                { upTo: 30, value: "" },
                                { upTo: 50, value: "orange" },
                                { upTo: 2000, value: "red" },
                            ],
                        },
                        ]
                    }
                ],
            }
        },
ottopaulsen commented 2 years ago

Ok, thanks. I am not sure when I get the time, but I will take a look at it.

ottopaulsen commented 2 years ago

I found time today :-) Version 1.1.1 has a fix for this. Please try it out. I just hope it does not have any side effects, but I think it will not. There were actually two bugs :-) One for making new connection on every refresh, and one for duplicating the servers on every refresh. Thank you for pointing this out.

ZoneMR commented 2 years ago

Brilliant - thanks - fix tested and works for me 🙂