3

When sending MQTT messages, first message arrives within 2 seconds and subsequent messages sent within less than 20 secs from first message, arrive in about 0.5 second.

I would like to speed up those “first” messages and wonder if there's a way to keep the line hot without bombarding the client with unnecessary keep alive kind of messages.

I am running Mosquitto version 2.0.4 on Ubuntu 18.04.5 LTS which has almost no load, it only runs the broker and has no subscribers other than the one I am testing.

The client is an Arduino 1400 MKR GSM connected over 3G network, it connects to the broker and then loops on the message receive callback without any other tasks.

mosquitto.conf

pid_file /var/run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log
log_timestamp true
log_timestamp_format %Y-%m-%d %H:%M:%S
log_type all

include_dir /etc/mosquitto/conf.d

default.conf

allow_anonymous false
password_file /etc/mosquitto/passwd

listener 1883
protocol mqtt

listener 8883
protocol mqtt

listener 8000
protocol websockets

listener 9001
protocol websockets

listener 8083
protocol websockets
cafile /etc/nginx-sp/ssl/xxx.combined_crt
keyfile /etc/nginx-sp/ssl/xxx.key
certfile /etc/nginx-sp/ssl/xxx.combined_crt

EDIT

Publisher code Using phpMQTT:

$mqtt = new phpMQTT($server, $port, $client_id);
    if ($mqtt->connect(true, NULL, $username, $password)) {
        $mqtt->publish($topic, $payload, 0);
        $mqtt->close();
        return 1;
    }
2
  • Have you checked whether the delay is on the publisher or the receiver side of the broker? It’s likely to be more on the receiver side, and that would be the interesting code. Do you use any power saving features?
    – jcaron
    Feb 11, 2021 at 17:28
  • The receiver is an Arduino with ArduinoMqttClient library. After connection established it awaits MQTT messages, it has no power saving mode and in fact no other code which may slow it down.
    – Nino
    Feb 13, 2021 at 10:47

1 Answer 1

2

I wonder if the publishing client is doing its entire sequence of connect and security handshakes at the time when you want to send your first message. The connect and handshakes can take some time over cellular networks, especially 3G. Perhaps you can share the code of the publisher. That would help.

In regards to your second question, Mosquitto does have keep alive functionality. The keepalive parameter is sometimes a parameter in the connect method of the MQTT client library, such as Mosquitto.

4
  • 1) Publisher code added to question. 2) As I wrote "keep alive kind of messages" may be confusing, I am fully aware of the keepalive mechanism but I don't think it helps here.
    – Nino
    Feb 11, 2021 at 6:28
  • As I thought, the code is doing the connect and following it immediately with publish. Is there some way you could do the connect earlier and then publish when there's something to publish ? Feb 11, 2021 at 20:19
  • This was a good point, however, it only saves 0.25 Sec which is not enough if taking in consideration the complications it may bring to separate connect from publish.
    – Nino
    Feb 12, 2021 at 12:50
  • It's also probably worth profiling the time take for the DNS lookup on the first connect.
    – hardillb
    Sep 16, 2022 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.