Method and apparatus for adjusting MQTT heartbeat frequency

By detecting the number of timeouts between the MQTT client and the server, adaptively adjusting the heartbeat frequency, the problem that the fixed frequency cannot adapt to different network environments is solved, and the effect of reducing costs and improving user experience is achieved.

WO2025112275A1PCT designated stage expired Publication Date: 2025-06-05HANGZHOU TUYA INFORMATION TECH CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2024/088474
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-11-27
Filing Date
2024-04-18
Publication Date
2025-06-05

AI Technical Summary

Technical Problem

In the prior art, the fixed ping/pong frequency of the MQTT client cannot adapt to different network environments, resulting in high traffic costs and server costs, or causing frequent equipment to be offline.

Method used

By detecting the number of timeouts between the MQTT client and the server in the preset time period, the heartbeat frequency is adaptively adjusted so that while ensuring the survival state, it is reduced to a lower level suitable for the current network environment.

Benefits of technology

It effectively reduces traffic costs and server costs, reduces the probability of MQTT clients offline, and thus improves the user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024088474_05062025_PF_FP_ABST
    Figure CN2024088474_05062025_PF_FP_ABST
Patent Text Reader

Abstract

The present application relates to a method and apparatus for adjusting a MQTT heartbeat frequency. The method comprises: (a) detecting whether the number of timeouts between a MQTT client and a MQTT server within a preset time period at the current heartbeat frequency exceeds a preset value; (b) in response to the number of timeouts not exceeding the preset value, reducing the heartbeat frequency in a first set mode to obtain a first heartbeat frequency; (c) when the first heartbeat frequency is not lower than a first preset frequency threshold, setting the first heartbeat frequency as the current heartbeat frequency, and returning to step (a); and (d) when the number of timeouts exceeds the preset value for the first time, determining the heartbeat frequency last recorded before the current heartbeat frequency as an adjusted heartbeat frequency. The solution of the present application can adaptively adjust the heartbeat frequency of the MQTT client on the basis of the current network environment, such that the heartbeat frequency of the MQTT client is maintained at a lower level suitable for the current network environment on the premise of guaranteeing keepalive.
Need to check novelty before this filing date? Find Prior Art

Description

Method and device for adjusting MQTT heartbeat frequency Technical Field

[0001] The present application relates to the field of computer software technology, and in particular to a method and device for adjusting the MQTT heartbeat frequency. Background Art

[0002] A large number of IoT (Internet of Things) devices use the MQTT (Message Queuing Telemetry Transport) protocol to transmit control commands and data. Most devices transmit data and commands infrequently, consuming significant resources in the ping / pong signals used to maintain keepalives. Currently, the ping / pong frequency of IoT devices is fixed and remains unchanged after leaving the factory.

[0003] However, the inventors found that a fixed ping / pong frequency cannot adapt to the actual network environment. Either the frequency is too high, resulting in higher traffic and server costs; or the frequency is too low, resulting in the intermediate NAT (Network Address Translation) conversion device being unable to maintain the MQTT connection, causing the device to frequently go online.

[0004] Summary of the Invention

[0005] In response to the problems caused by the fixed ping / pong frequency of MQTT clients (such as IoT devices) in the prior art, the present application provides a solution for adjusting the MQTT heartbeat frequency. This solution can adaptively adjust the heartbeat frequency of the MQTT client according to the current network environment, that is, adjust the ping / pong frequency so that the heartbeat frequency of the MQTT client can be maintained at a low level suitable for the current network environment while ensuring survival.

[0006] According to a first aspect of the present application, a method for adjusting the MQTT heartbeat frequency is provided, comprising:

[0007] (a) Detect whether the number of timeouts between the MQTT client and the MQTT server exceeds a preset value within a preset time period and at the current heartbeat frequency;

[0008] (b) in response to the number of timeouts not exceeding the preset value, reducing the heart rate in a first set manner to obtain a first heart rate;

[0009] (c) if the first heart rate is not lower than a first preset frequency threshold, setting the first heart rate as the current heart rate, and returning to step (a); and

[0010] (d) When the number of timeouts exceeds the preset value for the first time, the heartbeat frequency recorded last time before the current heartbeat frequency is determined as the adjusted heartbeat frequency.

[0011] According to a second aspect of the present application, a device for adjusting the MQTT heartbeat frequency is provided, characterized by comprising:

[0012] A detection module is used to detect whether the number of timeouts between the MQTT client and the MQTT server exceeds a preset value within a preset time period and at the current heartbeat frequency;

[0013] a heartbeat frequency reducing module, configured to reduce the heartbeat frequency in a first set manner to obtain a first heartbeat frequency in response to the number of timeouts not exceeding the preset value;

[0014] a first setting module, configured to set the first heartbeat frequency as the current heartbeat frequency if the first heartbeat frequency is not lower than a first preset frequency threshold; and

[0015] The first determining module is configured to determine the heartbeat frequency recorded last time before the current heartbeat frequency as the adjusted heartbeat frequency when the number of timeouts exceeds the preset value for the first time.

[0016] According to a third aspect of the present application, an electronic device is provided, including:

[0017] processor; and

[0018] The memory stores computer instructions, and when the computer instructions are executed by the processor, the processor is caused to perform the method described in the first aspect.

[0019] According to a fourth aspect of the present application, a non-transitory computer storage medium is provided, storing a computer program, which, when executed by multiple processors, enables the processors to execute the method described in the first aspect.

[0020] The method and apparatus for adjusting the MQTT heartbeat frequency provided in this application can reduce or increase the heartbeat frequency based on the timeout of the MQTT client in the current network environment, thereby enabling the MQTT client's heartbeat frequency to be adaptively adjusted based on the current network environment. While ensuring that the MQTT client remains alive, the heartbeat frequency is minimized, thereby reducing traffic and server costs. This significantly reduces costs while also reducing the probability of the MQTT client going offline, thereby improving the user experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without exceeding the scope of protection required by this application.

[0022] FIG1 is a flowchart of a process of an MQTT client in the case of frequent timeouts according to an embodiment of the present application.

[0023] FIG2 is a flowchart of a process of an MQTT client in stable operation according to an embodiment of the present application.

[0024] FIG3 is a flowchart of a method for adjusting the MQTT heartbeat frequency according to the first embodiment of the present application.

[0025] FIG4 is a flowchart of a method for adjusting the MQTT heartbeat frequency according to a second embodiment of the present application.

[0026] FIG5 is a flowchart of a method for adjusting the MQTT heartbeat frequency according to a third embodiment of the present application.

[0027] FIG6 is a flowchart of a method for adjusting the MQTT heartbeat frequency according to a fourth embodiment of the present application.

[0028] FIG7 is a flowchart of a method for adjusting the MQTT heartbeat frequency according to a fifth embodiment of the present application.

[0029] FIG8 is a flowchart of a method for adjusting the MQTT heartbeat frequency according to a sixth embodiment of the present application.

[0030] FIG9 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to the first embodiment of the present application.

[0031] FIG10 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a second embodiment of the present application.

[0032] FIG11 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a third embodiment of the present application.

[0033] FIG12 is a schematic diagram of a device for adjusting the MQTT heartbeat frequency according to a fourth embodiment of the present application.

[0034] FIG13 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a fifth embodiment of the present application.

[0035] FIG14 is a schematic diagram of a device for adjusting the MQTT heartbeat frequency according to a sixth embodiment of the present application.

[0036] FIG15 is a structural diagram of an electronic device provided in this application. DETAILED DESCRIPTION

[0037] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without making creative efforts are within the scope of protection of this application.

[0038] The overall idea of ​​this application is as follows: first, set an initial heartbeat frequency for the MQTT client, and monitor the ping / pong behavior of the MQTT client. Ping / pong are two messages used by the MQTT protocol for keepalive. Among them, ping is sent by the client to the server, and pong is the response of the server to the ping request. After the ping is sent, a pong will be received immediately. The interval time is the round-trip time (RTT) from the client to the server. RTT can be simply understood as the time it takes for a network request to go from the starting point to the destination and then back to the starting point. RTT is the time it takes to complete a heartbeat. 1 ping / pong is the RTT, and 2 ping / pongs are the heartbeat interval, that is, keepavlie timeout. Heartbeat frequency = 1 / heartbeat interval. If Keepalive timeouts occur frequently, increase the heartbeat frequency; and if the MQTT client runs stably for a long time, try to find a heartbeat frequency with a lower frequency and no Keepalive timeout. Among them, the MQTT client can be an IoT device or other types of devices that require MQTT services.

[0039] Figure 1 shows the processing flow of the MQTT client in the case of frequent timeouts. First, in order to determine whether the MQTT client frequently times out, it is necessary to define what circumstances constitute "frequent timeouts." According to some embodiments, it can be stipulated that if the number of timeouts occurring within a preset time period at the current heartbeat frequency exceeds a preset value, it can be deemed that the MQTT client frequently times out. For example, the initial heartbeat time is T, and the preset value is 1. It can be stipulated that if the number of timeouts exceeds 1 (for example, 2) within 10 times the initial heartbeat time T, it can be deemed that the MQTT client frequently times out.

[0040] If it is determined that the MQTT client frequently times out, the heartbeat time can be reset, for example, the heartbeat time is set to half of the current heartbeat time (corresponding to the initial heartbeat time). If the current heartbeat time is the initial heartbeat time T, then the reset heartbeat time is T / 2, so the reset heartbeat frequency is twice the current heartbeat frequency. Then, at the reset heartbeat frequency, determine whether the MQTT client frequently times out. If it still times out frequently, the heartbeat time can be reset to half of the current heartbeat time. If the current heartbeat time is T / 2, then the reset heartbeat time is T / 4, and the reset heartbeat frequency is twice the current heartbeat frequency. Then, continue to determine whether the MQTT client frequently times out based on the reset heartbeat frequency.

[0041] The above steps are repeated repeatedly until the MQTT client experiences no frequent timeouts at the newly set heartbeat frequency for the first time. The current heartbeat frequency (corresponding to the newly set heartbeat frequency) is then determined as the adjusted heartbeat frequency. When the MQTT client reconnects to the service, the heartbeat frequency is set to the adjusted heartbeat frequency, and the corresponding survival time is set to the time corresponding to the adjusted heartbeat frequency.

[0042] While continuously increasing the frequency and searching for the first heartbeat frequency that does not frequently timeout the MQTT client, the heartbeat frequency increase is not unlimited and cannot exceed the preset heartbeat frequency or heartbeat time range. For example, if the heartbeat time is preset to a minimum of 20 seconds and a maximum of 1800 seconds, the set heartbeat frequency cannot be higher than the frequency corresponding to the shortest heartbeat time. If the set heartbeat frequency is higher than the frequency corresponding to the shortest heartbeat time, the set heartbeat frequency will be fixed at the frequency corresponding to the shortest heartbeat time and will not be increased any further.

[0043] Figure 2 illustrates the process flow of an MQTT client under stable operation. According to some embodiments, stable operation can be determined when the MQTT client does not frequently timeout. For example, assuming an initial heartbeat time of T and a preset value of 1, the MQTT client can be considered stable if the number of timeouts does not exceed 1 within 10 times the initial heartbeat time, T.

[0044] If the MQTT client is running stably, the heartbeat time can be reset. For example, the heartbeat time can be set to twice the current heartbeat time (corresponding to the initial heartbeat time). If the current heartbeat time is the initial heartbeat time T, the reset heartbeat time is 2T, so the reset heartbeat frequency is half of the current heartbeat frequency. Then, at the reset heartbeat frequency, determine whether the MQTT client is running stably. If it is still running stably, the heartbeat time can be reset to half of the current heartbeat time. If the current heartbeat time is 2T, the reset heartbeat time is 4T, and the reset heartbeat frequency is half of the current heartbeat frequency. Then, continue to determine whether the MQTT client is running stably based on the reset heartbeat frequency.

[0045] The above steps are repeated repeatedly until the MQTT client experiences frequent timeouts for the first time at the newly set heartbeat frequency. The adjusted heartbeat frequency is then determined to be the previous heartbeat frequency. For example, if the MQTT client experiences frequent timeouts for the first time at the second reset heartbeat frequency, the first reset heartbeat frequency is set as the adjusted heartbeat frequency. When the MQTT client reconnects to the service, the heartbeat frequency is set to the adjusted heartbeat frequency, and the corresponding keepalive time is set to the time corresponding to the adjusted heartbeat frequency.

[0046] While continuously reducing the frequency and searching for the heartbeat frequency corresponding to the first frequent timeouts on the MQTT client, the reduction in heartbeat frequency is not unlimited and cannot exceed the preset heartbeat frequency or heartbeat time range. For example, if the heartbeat time is preset to a minimum of 20 seconds and a maximum of 1800 seconds, the set heartbeat frequency cannot be lower than the frequency corresponding to the longest heartbeat time. If the set heartbeat frequency is lower than the frequency corresponding to the longest heartbeat time, the set heartbeat frequency will be fixed at the frequency corresponding to the longest heartbeat time and will not be reduced any further.

[0047] It will be understood by those skilled in the art that the preset time period for determining the number of timeouts can be set according to actual needs. Regarding the method of increasing and decreasing the heart rate, the above only provides an example and should not be understood as a limitation of the present application. Inspired by the above examples, those skilled in the art can think of other ways to increase and decrease the heart rate, which all fall within the scope of coverage of the present application. For example, the heart rate can be increased or decreased by other multiples, the heart rate can be increased or decreased by a set amplitude, the amplitude of increasing or decreasing the heart rate each time can also be different, and so on.

[0048] Based on the above description, according to one aspect of the present application, a method for adjusting the MQTT heartbeat frequency is provided. As shown in FIG3 , the method includes the following steps.

[0049] Step S301 , detecting whether the number of timeouts between the MQTT client and the MQTT server at the current heartbeat frequency within a preset time period exceeds a preset value.

[0050] According to some embodiments, it can be stipulated that if the number of timeouts at the current heartbeat frequency within a preset time period exceeds a preset value, the MQTT client is considered to have experienced frequent Keepalive timeouts. Otherwise, the MQTT client is considered to have experienced no frequent Keepalive timeouts, i.e., the MQTT client is in a stable operating state. If Keepalive timeouts occur frequently, the heartbeat frequency is increased; if MQTT has been operating stably for a long time, the heartbeat frequency is reduced, and an attempt is made to find a heartbeat frequency with a lower frequency that does not cause Keepalive timeouts.

[0051] Step S302: In response to the timeout number not exceeding the preset value, the heartbeat frequency is reduced in a first setting manner to obtain a first heartbeat frequency.

[0052] Step S303: If the first heartbeat frequency is not lower than a first preset frequency threshold, the first heartbeat frequency is set as the current heartbeat frequency, and the process returns to step S301.

[0053] Step S304: When the number of timeouts exceeds the preset value for the first time, the heartbeat frequency recorded before the current heartbeat frequency is determined as the adjusted heartbeat frequency.

[0054] When it is determined that the number of timeouts does not exceed the preset value, that is, when it is determined that the MQTT client is running stably, the heartbeat time can be reset, for example, the heartbeat time is set to twice the current heartbeat time (corresponding to the initial heartbeat time). If the current heartbeat time is the initial heartbeat time T, then the reset heartbeat time is 2T, so the reset heartbeat frequency is half of the current heartbeat frequency. Then, at the reset heartbeat frequency, determine whether the MQTT client is running stably. If it is still running stably, the heartbeat time can be reset, and the heartbeat time is set to half of the current heartbeat time. If the current heartbeat time is 2T, then the reset heartbeat time is 4T, and the reset heartbeat frequency is half of the current heartbeat frequency. Then, continue to judge whether the MQTT client is running stably based on the reset heartbeat frequency.

[0055] The above steps are repeated repeatedly until the MQTT client experiences frequent timeouts for the first time at the newly set heartbeat frequency. The adjusted heartbeat frequency is then determined to be the previous heartbeat frequency. For example, if the MQTT client experiences frequent timeouts for the first time at the second reset heartbeat frequency, the first reset heartbeat frequency is set as the adjusted heartbeat frequency. When the MQTT client reconnects to the service, the heartbeat frequency is set to the adjusted heartbeat frequency, and the corresponding keepalive time is set to the time corresponding to the adjusted heartbeat frequency.

[0056] Figure 4 is a flow chart of a method for adjusting the MQTT heartbeat frequency according to a second embodiment of the present application. Compared with Figure 3 , steps S401 to S404 of Figure 4 are the same as steps S301 to S304 of Figure 3 , except that Figure 4 further includes the following steps.

[0057] Step S405, in response to the timeout number exceeding the preset value, increasing the heart rate in a second setting manner to obtain a second heart rate;

[0058] Step S406, if the second heartbeat frequency is not higher than the second preset frequency threshold, setting the second heartbeat frequency as the current heartbeat frequency, and returning to step S401; and

[0059] Step S407: When the number of timeouts does not exceed the preset value for the first time, the current heartbeat frequency is determined as the adjusted heartbeat frequency.

[0060] According to some embodiments, when it is determined that the MQTT client frequently times out, the heartbeat time can be reset, for example, the heartbeat time is set to half of the current heartbeat time (corresponding to the initial heartbeat time). If the current heartbeat time is the initial heartbeat time T, then the reset heartbeat time is T / 2, so that the reset heartbeat frequency is twice the current heartbeat frequency. Then, at the reset heartbeat frequency, determine whether the MQTT client frequently times out. If it still times out frequently, the heartbeat time can be reset, and the heartbeat time is set to half of the current heartbeat time. If the current heartbeat time is T / 2, then the reset heartbeat time is T / 4, and the reset heartbeat frequency is twice the current heartbeat frequency. Then, continue to judge whether the MQTT client frequently times out based on the reset heartbeat frequency.

[0061] The above steps are repeated repeatedly until the MQTT client experiences no frequent timeouts at the newly set heartbeat frequency for the first time. The current heartbeat frequency (corresponding to the newly set heartbeat frequency) is then determined as the adjusted heartbeat frequency. When the MQTT client reconnects to the service, the heartbeat frequency is set to the adjusted heartbeat frequency, and the corresponding survival time is set to the time corresponding to the adjusted heartbeat frequency.

[0062] Figure 5 is a flow chart of a method for adjusting the MQTT heartbeat frequency according to a third embodiment of the present application. Compared with Figure 3 , steps S501 to S504 of Figure 5 are the same as steps S301 to S304 of Figure 3 , except that Figure 5 further includes the following steps.

[0063] Step S505 , when the MQTT client reconnects to the service, communication between the MQTT client and the MQTT server is established at the adjusted heartbeat frequency.

[0064] Regardless of whether the heartbeat frequency is increased or decreased, when the MQTT client connects to the service again, the heartbeat frequency is set to the adjusted heartbeat frequency, and the corresponding survival time is set to the time corresponding to the adjusted heartbeat frequency, and the communication between the MQTT client and the MQTT server is established at the adjusted heartbeat frequency.

[0065] Figure 6 is a flow chart of a method for adjusting the MQTT heartbeat frequency according to a fourth embodiment of the present application. Compared with Figure 3 , steps S601 to S604 of Figure 6 are the same as steps S301 to S304 of Figure 3 , except that Figure 6 further includes the following steps.

[0066] Step S605 : before reducing the heartbeat frequency in the first setting manner for the first time, establishing a new connection between the MQTT client and another MQTT server.

[0067] According to some embodiments, if the MQTT client does not time out, before determining to lower the heartbeat frequency, a new connection can be established between the MQTT client and another MQTT server, the heartbeat frequency can be lowered in the new connection, and the timeout can be detected. During this process, the MQTT client and the original MQTT server still maintain a connection and maintain communication at the originally set heartbeat frequency. As shown in Figure 2, after determining the adjusted heartbeat frequency, when the MQTT client reconnects to the original MQTT server, the heartbeat frequency is set to the adjusted heartbeat frequency, and communication between the MQTT client and the original MQTT server is established at the adjusted heartbeat frequency.

[0068] Figure 7 is a flow chart of a method for adjusting the MQTT heartbeat frequency according to a fifth embodiment of the present application. Compared with Figure 3 , steps S701 to S704 of Figure 7 are the same as steps S301 to S304 of Figure 3 , except that Figure 7 further includes the following steps.

[0069] Step 705: When the first heartbeat frequency is lower than the first preset frequency threshold, determine the first heartbeat frequency as the first preset frequency threshold.

[0070] According to some embodiments, in the process of continuously reducing the frequency and looking for the heartbeat frequency corresponding to the first frequent timeout of the MQTT client, the reduction of the heartbeat frequency is not unlimited and cannot exceed the range of the preset heartbeat frequency or heartbeat time, and the preset minimum heartbeat frequency is set to the first preset frequency threshold. For example, the heartbeat time is preset to a minimum of 20 seconds and a maximum of 1800 seconds, so the set heartbeat frequency cannot be lower than the frequency corresponding to the longest heartbeat time. In the case that the set heartbeat frequency is lower than the preset minimum heartbeat frequency, the set heartbeat frequency is determined to be the frequency corresponding to the longest heartbeat time, and the heartbeat frequency is no longer reduced.

[0071] FIG8 is a flowchart of a method for adjusting the MQTT heartbeat frequency according to a sixth embodiment of the present application. Compared with FIG4 , steps S801 to S807 of FIG8 are the same as steps S401 to S407 of FIG4 , except that FIG8 further includes the following steps.

[0072] Step S808, when the second heartbeat frequency is higher than the second preset frequency threshold, the second heartbeat frequency is determined as the second preset frequency threshold. According to some embodiments, in the process of continuously increasing the frequency and looking for the heartbeat frequency corresponding to the first time when the MQTT client does not frequently timeout, the increase in the heartbeat frequency is not unlimited and cannot exceed the range of the preset heartbeat frequency or heartbeat time, and the preset maximum heartbeat frequency is set to the second preset frequency threshold. For example, the heartbeat time is preset to a minimum of 20 seconds and a maximum of 1800 seconds, then the set heartbeat frequency cannot be higher than the frequency corresponding to the shortest heartbeat time. In the case that the set heartbeat frequency is higher than the preset maximum heartbeat frequency, the set heartbeat frequency is determined to be the frequency corresponding to the shortest heartbeat time, and the heartbeat frequency is no longer increased.

[0073] On the basis of the above description, according to another aspect of the present application, a device for adjusting the MQTT heartbeat frequency is provided. As shown in Figure 9, the device includes: a detection module 901, a heartbeat frequency reduction module 902, a first setting module 903 and a first determination module 904. Among them, the detection module 901 is used to detect whether the number of timeouts between the MQTT client and the MQTT server at the current heartbeat frequency within a preset time period exceeds a preset value; the heartbeat frequency reduction module 902 is used to reduce the heartbeat frequency in a first setting manner in response to the number of timeouts not exceeding the preset value to obtain a first heartbeat frequency; the first setting module 903 is used to set the first heartbeat frequency to the current heartbeat frequency when the first heartbeat frequency is not lower than the first preset frequency threshold; the first determination module 904 is used to determine the heartbeat frequency recorded before the current heartbeat frequency as the adjusted heartbeat frequency when the number of timeouts exceeds the preset value for the first time.

[0074] FIG10 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a second embodiment of the present application. Compared to FIG9 , modules 1001 to 1004 of FIG10 are identical to modules 901 to 904 of FIG9 , except that FIG10 further includes: a heartbeat frequency increasing module 1005, a second setting module 1006, and a second determination module 1007. The heartbeat frequency increasing module 1005 is configured to, in response to the number of timeouts exceeding the preset value, increase the heartbeat frequency in a second setting manner to obtain a second heartbeat frequency; the second setting module 1006 is configured to, if the second heartbeat frequency is not higher than a second preset frequency threshold, set the second heartbeat frequency as the current heartbeat frequency; and the second determination module 1007 is configured to, if the number of timeouts does not exceed the preset value for the first time, determine the current heartbeat frequency as the adjusted heartbeat frequency.

[0075] FIG11 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a third embodiment of the present application. Compared to FIG9 , modules 1101 to 1104 of FIG11 are identical to modules 901 to 904 of FIG9 , except that FIG11 further includes a communication establishment module 1105 for establishing communication between the MQTT client and the MQTT server at the adjusted heartbeat frequency when the MQTT client reconnects to the service.

[0076] FIG12 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a fourth embodiment of the present application. Compared to FIG9 , modules 1201 through 1204 of FIG12 are identical to modules 901 through 904 of FIG9 , except that FIG12 further includes a connection establishment module 1205 for establishing a new connection between the MQTT client and another MQTT server before the heartbeat frequency is first reduced in the first setting manner.

[0077] FIG13 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a fifth embodiment of the present application. Compared to FIG9 , modules 1301 to 1304 of FIG13 are identical to modules 901 to 904 of FIG9 , except that FIG13 further includes a third determination module 1305 for determining the first heartbeat frequency as the first preset frequency threshold when the first heartbeat frequency is lower than the first preset frequency threshold.

[0078] FIG14 is a schematic diagram of an apparatus for adjusting the MQTT heartbeat frequency according to a sixth embodiment of the present application. Compared to FIG10 , modules 1401 through 1407 of FIG14 are identical to modules 1001 through 1007 of FIG10 , except that FIG14 further includes a fourth determination module 1408 for determining the second heartbeat frequency as the second preset frequency threshold if the second heartbeat frequency is higher than the second preset frequency threshold.

[0079] The method and apparatus for adjusting the MQTT heartbeat frequency provided in this application can reduce or increase the heartbeat frequency based on the timeout of the MQTT client in the current network environment, thereby enabling the MQTT client's heartbeat frequency to be adaptively adjusted based on the current network environment. While ensuring that the MQTT client remains alive, the heartbeat frequency is minimized, thereby reducing traffic and server costs. This significantly reduces costs while also reducing the probability of the MQTT client going offline, thereby improving the user experience.

[0080] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0081] It should be noted that for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that this application is not limited by the order of the actions described, because according to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in this specification are all optional embodiments, and the actions and modules involved are not necessarily required by this application.

[0082] In the several embodiments provided in this application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, and the indirect coupling or communication connection of the devices or units can be electrical connection or other forms.

[0083] Referring to FIG15 , FIG15 provides an electronic device including a processor and a memory. The memory stores computer instructions, which, when executed by the processor, enable the processor to execute the computer instructions to implement the method and detailed solutions shown in FIG3 to FIG8 .

[0084] It should be understood that the above-described device embodiments are merely illustrative, and the devices disclosed herein may also be implemented in other ways. For example, the division of units / modules described in the above-described embodiments is merely a logical functional division, and actual implementations may employ alternative divisions. For example, multiple units, modules, or components may be combined or integrated into another system, or some features may be omitted or not implemented.

[0085] In addition, unless otherwise specified, the functional units / modules in the various embodiments of the present invention may be integrated into a single unit / module, each unit / module may exist physically separately, or two or more units / modules may be integrated together. The aforementioned integrated units / modules may be implemented in the form of hardware or software program modules.

[0086] If the integrated unit / module is implemented in hardware, the hardware may be a digital circuit, an analog circuit, or the like. The physical implementation of the hardware structure includes, but is not limited to, transistors, memristors, and the like. Unless otherwise specified, the processor or chip may be any appropriate hardware processor, such as a CPU, GPU, FPGA, DSP, and ASIC. Unless otherwise specified, the on-chip cache, off-chip memory, and storage may be any appropriate magnetic storage medium or magneto-optical storage medium, such as resistive random access memory (RRAM), dynamic random access memory (DRAM), static random access memory (SRAM), enhanced dynamic random access memory (EDRAM), high-bandwidth memory (HBM), hybrid memory cube (HMC), and the like.

[0087] If the integrated unit / module is implemented in the form of a software program module and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a memory, including a number of instructions for enabling a computer electronic device (which can be a personal computer, a server or a network electronic device, etc.) to perform all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned memory includes: various media that can store program codes, such as a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk.

[0088] An embodiment of the present application also provides a non-transitory computer storage medium storing a computer program. When the computer program is executed by multiple processors, the processors execute the method and detailed solutions shown in Figures 3 to 8.

[0089] The embodiments of the present application are described in detail above. Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only intended to help understand the method and core ideas of the present application. At the same time, changes or modifications made by those skilled in the art based on the ideas of the present application, the specific implementation methods, and the scope of application of the present application, all fall within the scope of protection of the present application. In summary, the contents of this specification should not be construed as limiting the present application.

Claims

1. A method for adjusting MOTT heart rate, characterized in that: include: (a) Detect whether the number of timeouts between the MOTT client and the MOTT server at the current heartbeat frequency within a preset time period exceeds a preset value; (b) in response to the number of timeouts not exceeding the preset value, reducing the heart rate in a first setting manner to obtain a first heart rate; (c) if the first heart rate is not lower than a first preset frequency threshold, setting the first heart rate as the current heart rate, and returning to step (a); and (d) When the number of timeouts exceeds the preset value for the first time, the heart rate recorded last time before the current heart rate is determined as the adjusted heart rate.

2. The method according to claim 1, characterized in that Also includes: (e) in response to the number of timeouts exceeding the preset value, increasing the heart rate in a second setting manner to obtain a second heart rate; (f) if the second heart rate is not higher than a second preset frequency threshold, setting the second heart rate as the current heart rate, and returning to step (a); and (g) when the number of timeouts does not exceed the preset value for the first time, determining the current heartbeat frequency as the adjusted heartbeat frequency.

3. The method according to claim 1, characterized in that Also includes: Before the heartbeat frequency is reduced in the first setting manner for the first time, a new connection is established between the MOTT client and another MOTT server.

4. The method according to claim 1, characterized in that Also includes: When the first heartbeat frequency is lower than the first preset frequency threshold, the first heartbeat frequency is determined as the first preset frequency threshold.

5. The method according to claim 2, characterized in that Also includes: When the second heartbeat frequency is higher than the second preset frequency threshold, The second heartbeat frequency is determined as the second preset frequency threshold.

6. The method according to any one of claims 1 to 5, characterized in that: Also includes: When the MOTT client connects to the service again, communication between the MQTT client and the MQTT server is established at the adjusted heartbeat frequency.

7. The method according to any one of claims 1 to 5, characterized in that: The first setting method includes reducing the heart rate by a set multiple, and the second setting method includes increasing the heart rate by a set multiple.

8. A device for adjusting MOTT heart rate, characterized in that: include: A detection module is used to detect whether the number of timeouts between the MQTT client and the MQTT server exceeds a preset value within a preset time period and at a current heartbeat frequency; A heartbeat frequency reduction module, configured to reduce the heartbeat frequency in a first setting manner in response to the number of timeouts not exceeding the preset value, to obtain a first heartbeat frequency; A first setting module, configured to set the first heartbeat frequency as the current heartbeat frequency when the first heartbeat frequency is not lower than a first preset frequency threshold; as well as The first determining module is used to determine the heartbeat frequency recorded before the current heartbeat frequency as the adjusted heartbeat frequency when the timeout number exceeds the preset value for the first time.

9. An electronic device, characterized in that: The method comprises a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the method according to any one of claims 1 to 7 when executing the computer program in the memory.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Heartbeat keep-alive method and apparatus for long connection in communication network

    CN105610913A

  • Network communication control method and device, communication equipment and storage medium

    CN111031598A

  • Heartbeat control method and device

    CN112987904A

  • Method and device for adjusting MQTT heartbeat frequency

    CN117294750A

  • Information processing device, failure determination method, cluster system, and program

    JP2017068309A