如何加速第一条 Mosquitto 消息?

物联网 MQTT 阿杜伊诺 蚊子
2021-06-25 10:04:40

发送 MQTT 消息时,第一条消息在 2 秒内到达,后续消息在第一条消息后不到 20 秒内发送,大约在 0.5 秒内到达。

我想加快那些“第一个”消息的速度,并想知道是否有一种方法可以保持线路热,而不会用不必要的保持活动的消息轰炸客户端。

我在几乎没有负载的 Ubuntu 18.04.5 LTS 上运行 Mosquitto 2.0.4 版,它只运行代理并且除了我正在测试的订阅者之外没有其他订阅者。

客户端是通过 3G 网络连接的 Arduino 1400 MKR GSM,它连接到代理,然后在消息接收回调上循环,没有任何其他任务。

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

默认配置文件

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

编辑

发布者代码 使用 phpMQTT:

$mqtt = new phpMQTT($server, $port, $client_id);
    if ($mqtt->connect(true, NULL, $username, $password)) {
        $mqtt->publish($topic, $payload, 0);
        $mqtt->close();
        return 1;
    }
1个回答

我想知道在您想要发送第一条消息时发布客户端是否正在执行其整个连接和安全握手序列。通过蜂窝网络(尤其是 3G)进行连接和握手可能需要一些时间。也许您可以共享发布者的代码。那会有所帮助。

关于您的第二个问题,Mosquitto 确实具有保持活动功能。keepalive 参数有时是 MQTT 客户端库的 connect 方法中的一个参数,比如 Mosquitto。