High and Variable Latency in MQTT Communication Using the Industrial Communication Toolbox

22 views (last 30 days)
Hello, I'm using an MQTT client from the Industrial Communication Toolbox. I'm sending an input via Python to MATLAB over MQTT every 100ms. The latency between sending in Python and receiving in MATLAB averages about 60ms, but the latency fluctuates significantly. It seems like the latency keeps increasing until it reaches around 120ms delay, and then it drops back to 6ms. When I measure the latency in Python between sending and receiving, it's only 1ms.
I'm wondering why the latency is so high and variable in MATLAB. I need a consistent latency for my program, rather than such significant fluctuations. If anyone has any suggestions on how to address this issue, I would be very grateful.

Answers (2)

Sivapriya Srinivasan
Sivapriya Srinivasan on 5 Sep 2023
Hello ,
Here are a few potential causes and suggestions to address the issue:
  1. MATLAB processing time: MATLAB might have additional processing overhead compared to Python, which could contribute to the higher latency. To address this, you can optimize your MATLAB code to reduce processing time or consider parallelizing certain tasks to improve efficiency.
  2. MATLAB event queue: MATLAB might be using an event-driven model for handling MQTT messages, which can introduce variability in the latency. Check if there is an event queue or buffer in MATLAB that could be causing the fluctuations. You can try to increase the event queue size or implement a more efficient event handling mechanism to reduce latency variations.
  3. Network conditions: Fluctuations in network conditions, such as network congestion or packet loss, can impact the latency of MQTT messages. Check your network setup and ensure that it is stable and reliable. You can also try monitoring the network traffic during the latency spikes to identify any potential issues.
  4. MQTT client configuration: Review the configuration options of the MQTT client in MATLAB. There might be settings related to message handling, buffering, or QoS (Quality of Service) that could affect the latency.
  5. MATLAB and MQTT library versions: Ensure that you are using the latest versions of MATLAB and the MQTT library. Sometimes, software updates can include performance improvements or bug fixes that could help reduce latency variations.
  6. Hardware limitations: Consider the hardware resources of your system, such as CPU and memory usage. If the system is under heavy load or if the hardware is not sufficient, it can contribute to latency fluctuations. Optimize your system resources and consider upgrading hardware if necessary.
It is recommended to perform profiling and benchmarking tests on your MATLAB code to identify any potential bottlenecks or areas for optimization. Additionally, consult the documentation : MQTT API - MATLAB & Simulink (mathworks.com)
Hope this helps!

Ernst van der Pols
Ernst van der Pols on 10 Jan 2024
Hello Tobias,
I have referred to your question in my answer to my own question rearding MQTT v5 support in MATLAB. I am just starting with MQTT in MATLAB, so I cannot confirm your issues (yet) or confirm that I have a solution, but I (thanks to Gennaro Notomista) provide the steps to build your own wrapper around the Paho MQTT Java client.
In case of serious issues it might be the only solution to tweak the Java client, or replace it with other alternatives.

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!