A message transmission method and device based on MQTT protocol

By improving the MQTT protocol and adopting a one-to-one message sending and receiving and a mutual exclusion subscription mechanism, the problems of data redundancy and inconsistency in the MQTT protocol are solved, and efficient and reliable message transmission is achieved, which is suitable for the Internet of Things and small devices.

CN116846959BActive Publication Date: 2026-02-13NEUSOFT RUICHI AUTOMOTIVE TECH (DALIAN) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310834349.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-07-07
Publication Date
2026-02-13
Estimated Expiration
2043-07-07

AI Technical Summary

Technical Problem

The existing MQTT protocol suffers from problems such as data redundancy, inconsistency, and conflict in IoT and small devices, especially when multiple subscribers or publishers operate simultaneously, resulting in wasted network resources and data inconsistency.

Method used

The message sending and receiving method based on the MQTT protocol is adopted. Through a one-to-one message sending and receiving mode and a mutual exclusion subscription mechanism, the exclusive subscription of topics is achieved by using cloud point-to-point mode and P2P communication, which avoids multiple subscribers from subscribing to the same topic at the same time, thereby reducing network overhead and push latency.

Benefits of technology

It improves the efficiency and quality of data transmission, solves the problems of data redundancy, inconsistency and conflict, and achieves efficient and reliable message transmission.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116846959B_ABST
    Figure CN116846959B_ABST
Patent Text Reader

Abstract

The application provides a message transmission method and device based on an MQTT protocol, the method comprising: a message sender sending a target message to an MQTT server; the MQTT server sending the target message to a target receiver according to a message type and a client identifier of the target receiver; the target receiver creating a request subscription topic as a subscriber; the MQTT server judging whether a subscription type is an exclusive subscription, the exclusive subscription being used to represent that only one subscriber is allowed to exist at the same time for a topic; when the subscription type is the exclusive subscription, the MQTT server establishing a mutual exclusion lock on the topic to reject a subscription request of a client other than the target receiver; and the message sender establishing a mutual exclusion lock according to the topic and taking the topic as a publishing topic of the message sender, so that the topic is only subscribed by the target receiver. The efficiency and quality of data transmission are improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present application relate to the technical field of information transmission, and in particular to a message transceiving method and device based on MQTT protocol. BACKGROUND

[0002] MQTT (Message Queuing Telemetry Transport) is a lightweight communication protocol based on publish / subscribe mode, and has a wide range of applications in Internet of Things, small devices, mobile applications, etc.

[0003] However, the current data publishing and subscribing mechanism has some problems, such as data redundancy, data inconsistency, data conflict, etc. For example, when multiple subscribers subscribe to the same topic at the same time, it may cause waste and congestion of network resources, or lead to data out of sync or inconsistent between different subscribers. In addition, when multiple publishers publish the same topic at the same time, it may cause data conflict or incompatibility between different publishers. SUMMARY

[0004] The present application provides a message transceiving method and device based on MQTT protocol, which can realize one-to-one message transceiving and mutual exclusive subscription of topics, and improve the efficiency and quality of data transmission.

[0005] In a first aspect, a message transceiving method based on MQTT protocol is provided, which includes: a message sender sending a target message to an MQTT server, the target message including a message topic and message content, the message topic carrying a message type and a client identifier of a target receiver; the MQTT server sending the target message to the target receiver according to the message type and the client identifier of the target receiver; the target receiver creating a requested subscription topic as a subscriber, the topic carrying a subscription type, the message type and a client identifier of a message sender; the MQTT server judging whether the subscription type is exclusive subscription, the exclusive subscription being used to represent that only one subscriber is allowed to exist for a topic at the same time; when the subscription type is exclusive subscription, the MQTT server establishes a mutual exclusion lock on the topic to reject the subscription request of a client other than the target receiver; the message sender establishes a mutual exclusion lock according to the topic as a publisher, and takes the topic as its own publishing topic, so that the topic is only subscribed by the target receiver.

[0006] Preferably, before the message sender sends the target message to the MQTT server, it further includes:

[0007] The message sender constructs a two-level topic and a three-level topic according to a message type and a client identifier of a target receiver; the two-level topic is a message type, and the three-level topic is a client identifier of a target receiver; the two-level topic and the three-level topic are taken as the message topic, and the message content is taken as the load, to create the target message.

[0008] Preferably, the MQTT server sends the target message to the target receiver according to the message type and the client identifier of the target receiver, including: the MQTT server parses the target message to obtain the two-level topic and the three-level topic; it is judged whether the two-level topic is a preset message type; if the two-level topic is a preset message type, the client identifier of the target receiver is extracted from the three-level topic, and the target message is sent to the target receiver; or if the two-level topic is not the preset message type, the target message is ignored.

[0009] Preferably, the MQTT server sends the target message to the target receiver according to the message type and the client identifier of the target receiver, including: the MQTT server parses the target message to obtain the two-level topic and the three-level topic; it is judged whether the two-level topic is a preset message type; if the two-level topic is a preset message type, the client identifier of the target receiver is extracted from the three-level topic, and the target message is sent to the target receiver; or if the two-level topic is not the preset message type, the target message is ignored.

[0010] Preferably, the method further includes: the message sender and the target receiver perform one-to-one message transmission based on the topic, and perform confirmation or retransmission according to a preset quality of service level; after the message sender or the target receiver ends communication, a mutual exclusion lock on the topic is released, and a disconnection request is sent to the MQTT server; after the MQTT server receives the disconnection request, the mutual exclusion lock on the topic is released.

[0011] Preferably, the target receiver creates a topic to request subscription as a subscriber, the topic carries a subscription type, a message type and a client identification of a message sender, and the MQTT server checks whether the topic requested to be subscribed by the target receiver has been subscribed by other clients, and if the topic has been subscribed by other clients, the subscription request of the target receiver is rejected, or if the topic has not been subscribed by other clients, the subscription request of the target receiver is allowed, and the client of the target receiver is associated with the topic, so that each topic has only one subscriber; the MQTT server checks whether the topic requested to be subscribed by the target receiver has been unsubscribed, and if the topic has been unsubscribed by the target receiver, the association between the client of the target receiver and the topic is released, and other clients are allowed to subscribe to the topic.

[0012] Preferably, the method further comprises: after receiving the message of the target receiver for unsubscribing, the MQTT server releases the association between the client of the target receiver and the topic, so as to allow other clients to subscribe to the topic.

[0013] In a second aspect, a message transmission and reception device based on the MQTT protocol is provided, comprising:

[0014] a message sending module, configured to send a target message to an MQTT server, the target message comprising a message topic and message content, the message topic carrying a message type and a client identification of a target receiving module; the MQTT server is configured to send the target message to the target receiving module according to the message type and the client identification of the target receiving module; the target receiving module is configured to create a topic to request subscription as a subscriber, the topic carrying a subscription type, a message type and a client identification of a message sending module; the MQTT server is further configured to judge whether the subscription type is an exclusive subscription, the exclusive subscription being used to represent that a topic is allowed to have only one subscriber at the same time; when the subscription type is the exclusive subscription, the MQTT server is configured to establish a mutual exclusion lock on the topic, so as to reject the subscription request of a client other than the target receiving module; the message sending module is configured to establish a mutual exclusion lock according to the topic as a publisher, and to take the subscription topic as a publishing topic of itself, so that the topic is only subscribed by the target receiving module.

[0015] Preferably, the MQTT server comprises: a configuration module configured to configure whether to enable exclusive subscription in a configuration file; a checking module configured to check whether a topic requested to be subscribed by the target receiving module has been subscribed by other clients when the exclusive subscription is enabled; or check whether the topic requested to be subscribed by the target receiving module has been unsubscribed; a subscription module configured to allow the subscription request of the target receiving module and associate the client of the target receiving module with the topic; the subscription module is further configured to reject the subscription request of the client other than the target receiving module; and an unsubscription module configured to, after receiving a message of the target receiving module for unsubscription, disassociate the client of the target receiving module from the topic to allow other clients to subscribe to the topic.

[0016] Preferably, the MQTT server further comprises: a mutual exclusion lock management module configured to establish or release a mutual exclusion lock on the topic according to a subscription message or an unsubscription message, so that only one message sender and one target receiver are allowed to exist at the same time for each topic.

[0017] The technical scheme provided in the application comprises the following steps: a message sender sends a target message to an MQTT server; the MQTT server sends the target message to a target receiver according to a message type and a client identifier of the target receiver, so that a one-to-one message transmission mode can be realized without pre-arranging a topic or a subscription relationship, network overhead and push delay are reduced, and the efficiency and reliability of message transmission are improved; then the target receiver creates a topic requested to be subscribed as a subscriber; the MQTT server judges whether a subscription type is exclusive subscription, the exclusive subscription is used to represent that only one subscriber is allowed to exist at the same time for a topic; when the subscription type is exclusive subscription, the MQTT server establishes a mutual exclusion lock on the topic to reject the subscription request of the client other than the target receiver; and the message sender establishes a mutual exclusion lock according to the topic and takes the topic as a publishing topic of the message sender, so that the topic is only subscribed by the target receiver. By checking whether a topic requested to be subscribed by a certain client has been subscribed by other clients, the function of mutually exclusive subscription of the topic is realized, and problems such as data redundancy, data inconsistency and data conflict caused by simultaneous subscription of multiple subscribers to the same topic are avoided. BRIEF DESCRIPTION OF DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the embodiments of the application, the following will briefly introduce the drawings needed to be used in the embodiment description. Obviously, the drawings in the following description are only some embodiments of the application, and other drawings can also be obtained by those skilled in the art without any creative effort based on these drawings.

[0019] Figure 1 An application scenario graph provided in the embodiments of the application is shown in the following figure.

[0020] Figure 2 A flowchart illustrating a message sending and receiving method based on the MQTT protocol, provided in an embodiment of this application;

[0021] Figure 3 A flowchart illustrating another message sending and receiving method based on the MQTT protocol provided in this application embodiment;

[0022] Figure 4 A schematic diagram of a message sending and receiving device based on the MQTT protocol provided in this application embodiment;

[0023] Figure 5 yes Figure 4 A schematic block diagram of an MQTT server. Detailed Implementation

[0024] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.

[0025] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or server that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or devices.

[0026] As mentioned above, there are numerous data publishing and subscription scenarios in the field of connected vehicles, such as vehicles acquiring road condition information, navigation information, and entertainment information, or vehicles publishing their own status information, safety warnings, and emergency assistance information. These scenarios require effective management of the matching relationship between data publishers and subscribers to achieve accurate data delivery and consumption.

[0027] However, data publishing and subscription mechanisms have some problems, such as data redundancy, data inconsistency, and data conflicts. For example, when multiple subscribers subscribe to the same topic simultaneously, it may cause waste and congestion of network resources, or lead to data asynchrony or inconsistency between different subscribers. Additionally, when multiple publishers publish to the same topic simultaneously, it may cause data conflicts or incompatibility between different publishers.

[0028] To address the aforementioned technical problems, the inventive concept of this application is to utilize a cloud-based peer-to-peer model to achieve one-to-one message sending and receiving, eliminating the need for prior agreement on topics or subscription relationships, thereby reducing network overhead and push latency, and improving the efficiency and reliability of message transmission. Then, based on a new data publishing and subscription mechanism, topics are subject to mutually exclusive subscription (i.e., only one subscriber is allowed to exist for a topic at any given time) and P2P (peer to peer) communication to improve the efficiency and quality of data transmission.

[0029] It should be understood that the technical solution of this application can be applied to the following scenarios, but is not limited to:

[0030] In some possible ways, Figure 1 An application scenario diagram provided for an embodiment of this application, such as... Figure 1 As shown, the message sending and receiving system may include a first MQTT client 110, an MQTT server 120, and a second MQTT client 130. The first MQTT client 110 and the second MQTT client 130 can establish a connection with the MQTT server 120 through a wired network or a wireless network.

[0031] For example, the first MQTT client 110 and the second MQTT client 130 can be desktop computers, laptops, tablets, etc., but are not limited to these. The MQTT server 120 can be a terminal device or a server, but is not limited to these. In one embodiment of this application, the first MQTT client 110 can send a target message to the MQTT server 120 as a message sender. Further, the MQTT server 120 can receive the target message sent by the first MQTT client 110 and forward the target message to the second MQTT client 130. The second MQTT client 130 can receive the target message forwarded by the MQTT server 120 as a target receiver.

[0032] also, Figure 1 An exemplary first MQTT client 110 and a second MQTT client 130 are provided, but other numbers of clients may be included in practice, and this application does not limit this.

[0033] After introducing the application scenarios of the embodiments of the present application, the technical solutions of the present application will be described in detail as follows.

[0034] Figure 2 A flowchart of a message transceiving method based on the MQTT protocol provided by the embodiments of the present application can include the following steps:

[0035] S210: The message sender sends a target message to the MQTT server, and the target message includes a message topic and message content, and the message topic carries a message type and a client identifier of a target receiver.

[0036] In this step, the message sender can send the target message to the MQTT server through the first MQTT client, and the target message is an MQTT message, which includes two parts of a topic (Topic) and a payload. The message sender can first construct a two-level topic and a three-level topic according to the message type and the client identifier of the target receiver, wherein the two-level topic is the message type, and exemplarily, the two-level topic can be “p2p”; the three-level topic is the client identifier of the target receiver, and exemplarily, the three-level topic can be the client identifier “Client ID2” of the second MQTT client. Then, the message sender creates the target message by taking the two-level topic and the three-level topic as the message topic and taking the message content as the payload.

[0037] S220: The MQTT server sends the target message to the target receiver according to the message type and the client identifier of the target receiver.

[0038] In this embodiment, the target message can be sent to the target receiver through the second MQTT client. The MQTT server can first parse the target message to obtain the two-level topic and the three-level topic, and then determine whether the two-level topic is a preset message type. If the two-level topic is the preset message type, the client identifier of the target receiver is extracted from the three-level topic, and the target message is sent to the target receiver. If the two-level topic is not the preset message type, the target message is ignored.

[0039] Exemplarily, the preset message type can be the “p2p” type set by a human being in advance. The MQTT server determines whether the two-level topic is “p2p”. If yes, the target message is sent to the target receiver. If no, the message is ignored or processed according to the publish / subscribe mode. After the target receiver is connected to the MQTT server through the second MQTT client, the target receiver does not need to subscribe to any topic, and can directly receive the P2P message from the message sender from the MQTT server.

[0040] S230: The target receiver creates a requested subscription topic as a subscriber, and the topic carries a subscription type, a message type and a client identifier of a message sender.

[0041] In this step, after receiving the target message, the target receiver can send a confirmation message to the MQTT server, and create a topic as a subscriber to request subscription, which carries the subscription type, message type and client identifier of the message sender. For example, the topic for requesting subscription can be $exclusive / p2p / Client ID1, where "exclusive" indicates that the subscription type is exclusive subscription, "p2p" indicates that the message type is p2p message, and "Client ID1" indicates that the client identifier of the message sender is Client ID1.

[0042] S240: The MQTT server determines whether the subscription type is exclusive subscription, which is used to indicate that only one subscriber is allowed to exist at the same time for a topic.

[0043] It can be understood that the subscription type can include multiple types, such as normal subscription or exclusive subscription. The normal subscription is used to indicate that multiple subscribers are allowed to exist at the same time for a topic, and the exclusive subscription is used to indicate that only one subscriber is allowed to exist at the same time for a topic.

[0044] S250: When the subscription type is exclusive subscription, the MQTT server establishes a mutex on the topic to reject the subscription request of the client other than the target receiver.

[0045] S260: The message sender establishes a mutex on the topic according to the topic, and uses the topic as its own publishing topic, so that the topic is only subscribed by the target receiver.

[0046] In this embodiment, after receiving the confirmation message of the target receiver, the MQTT server can establish a mutex on the topic $exclusive / p2p / Client ID1, and forward the confirmation message to the message sender. The confirmation message is used to indicate that the target receiver has received the target message. After receiving the confirmation message, the message sender can also establish a mutex on $exclusive / p2p / Client ID1, and use the topic as its own publishing topic, so that the message sender and the target receiver perform one-to-one message transmission on $exclusive / p2p / Client ID1, and perform confirmation or retransmission according to the pre-set or negotiated quality of service (QoS) level. The reliability guarantee in the P2P communication process is realized, so that each message can be delivered to the destination according to different QoS levels.

[0047] Further, after the message sender or the target receiver ends the communication, the mutex lock on the topic can be released, and a disconnection request can be sent to the MQTT server; after receiving the disconnection request, the MQTT server can release the mutex lock on the topic.

[0048] Further, the client identifier of the target receiver is extracted from the three-level topic, and the target message is sent to the target receiver, and the method further comprises:

[0049] S310: The MQTT server checks whether the target receiver is online according to the client identifier of the target receiver in the client connection table.

[0050] S320: If the target receiver is online, the target message is sent to the target receiver and a result of successful forwarding is returned; or if the target receiver is not online, the target message is stored in the offline message table, and a result of successful storage is returned; the offline message table stores the client identifier of each offline client and a corresponding unread message list.

[0051] When the target receiver is online, the MQTT server finds the client identifier of the target receiver and the unread message list in the offline message table, and forwards all the messages in the unread message list to the target receiver in sequence until the unread message list is empty.

[0052] In the embodiment, the message transceiving method based on the P2P mode of the MQTT protocol can realize one-to-one message transceiving mode, without pre-arranging topics or subscription relationships, thereby reducing network overhead and push delay, and improving the efficiency and reliability of message transmission. Meanwhile, the problems of high registration and subscription relationship cost, large message forwarding and distribution overhead, and message push delay loss in the prior art based on the publish / subscribe mode are solved, and the performance and quality of message transceiving are improved.

[0053] Figure 3 Another flowchart of a message transceiving method based on the MQTT protocol is provided in the embodiment. As shown in FIG. 2B, after step S230, the method further comprises: Figure 3

[0054] S270: The MQTT server checks whether the topic requested to be subscribed by the target receiver has been subscribed by other clients.

[0055] S280: If the topic has been subscribed by other clients, the subscription request of the target receiver is rejected; or if the subscription topic has not been subscribed by other clients, the subscription request of the target receiver is allowed, and the client of the target receiver is associated with the topic, so that each topic has only one valid subscriber.

[0056] ​S290: After receiving the message of the target receiver canceling the subscription, the MQTT server cancels the association between the client of the target receiver and the topic, to allow other clients to subscribe to the topic.

[0057] In this embodiment, whether to enable exclusive subscription can be configured in the configuration file in advance by the MQTT server or the configuration module, so that whether to use the exclusive subscription function can be selected according to different vehicle networking scenarios and requirements, to increase the adaptability and scalability of the system. If the exclusive subscription is enabled, whether the topic requested to be subscribed by a certain client has been subscribed by other clients is checked by the MQTT server or the checking module, so that the function of mutually exclusive subscription of the topic is realized, and problems such as data redundancy, data inconsistency, data conflict, etc. caused by simultaneous subscription of multiple subscribers to the same topic are avoided. Exemplarily, an exclusive subscription prefix can be set to identify the topic that needs to be exclusively subscribed. The MQTT server or the dynamic topic generation module can generate a dynamic topic according to the client identifier of the target receiver; for example, $exclusive / t / 1 can be used to represent the topic t / 1 that needs to be exclusively subscribed, so that the system can conveniently classify and filter the topics, and reduce the complexity and overhead of the system.

[0058] Further, the subscription request of a certain client can also be allowed or rejected by the MQTT server or the subscription module, and when the subscription request of a certain client is allowed, the client is associated with the topic; the association between a certain client and a certain topic can be released by the MQTT server or the unsubscription module, and other clients are allowed to subscribe to the topic. The function of dynamically updating the topic is realized, and it is ensured that each topic always has a latest subscriber.

[0059] Further, the MQTT server or the control message processing module can generate or parse the control message according to the dynamic topic, and interact with the MQTT server; then the MQTT server or the mutual exclusion lock management module can establish or release the mutual exclusion lock on the dynamic topic according to the control message. The mutual exclusion guarantee in the P2P communication process is realized, so that only one sender and one receiver are allowed to exist at the same time for each dynamic topic.

[0060] It should be noted that the configuration module, the checking module, the dynamic topic generation module, the subscription module, the unsubscription module, the control message processing module, the mutual exclusion lock management module, etc. in this embodiment can be integrated in another server, which can interact with the first MQTT client and the second MQTT client; in another embodiment, the above modules can also be arranged in the MQTT server, and the embodiment is not limited thereto.

[0061] Figure 4 A schematic diagram of a message transceiving device 400 based on the MQTT protocol provided by the embodiment of the application is shown in FIG. 4. As shown in FIG. 4, the message transceiving device 400 includes a processor 401, a memory 402, a transceiver 403, and a power supply 404.Figure 4 As shown, the apparatus 400 comprises:

[0062] a message sending module 410, configured to send a target message to an MQTT server 420, the target message comprising a message topic and a message content, the message topic carrying a message type and a client identifier of a target receiving module 430;

[0063] the MQTT server 420, configured to send the target message to the target receiving module 430 according to the message type and the client identifier of the target receiving module 430;

[0064] the target receiving module 430, configured to create a requested subscription topic as a subscriber, the topic carrying a subscription type, the message type and a client identifier of the message sending module 410;

[0065] the MQTT server 420 is further configured to determine whether the subscription type is an exclusive subscription, the exclusive subscription being used to represent that only one subscriber is allowed to exist for a topic at the same time;

[0066] when the subscription type is the exclusive subscription, the MQTT server 420 is configured to establish a mutual exclusion lock on the topic to reject a subscription request of a client other than the target receiving module 430;

[0067] the message sending module 410 is configured to establish a mutual exclusion lock according to the topic as a publisher, and use the topic as a publishing topic of itself, so that the topic is only subscribed by the target receiving module 430.

[0068] In some possible implementation manners, the MQTT server 420 further comprises:

[0069] a configuration module 4201, configured to configure whether to start the exclusive subscription in a configuration file;

[0070] a checking module 4202, configured to check whether a topic requested to be subscribed by the target receiving module 430 has been subscribed by other clients when the exclusive subscription is started, or check whether the topic requested to be subscribed by the target receiving module 430 has been unsubscribed;

[0071] a subscription module 4203, configured to allow a subscription request of the target receiving module 430, and associate a client of the target receiving module 430 with the topic; the subscription module 4203 is further configured to reject a subscription request of a client other than the target receiving module 430;

[0072] The unsubscribing module 4204 is configured to, after receiving the message of the target receiving module 430 for unsubscribing, disassociate the client of the target receiving module 430 from the topic to allow other clients to subscribe to the topic.

[0073] In some implementable manners, the MQTT server 420 further comprises:

[0074] The mutex management module 4205 is configured to establish or release a mutex on a topic according to a subscription message or an unsubscribing message, so that only one message sender and one target receiver are allowed to exist on each topic at the same time.

[0075] It should be understood that the device embodiment and the message communication method embodiment based on the MQTT protocol can correspond to each other, and similar descriptions can be referred to the message communication method embodiment based on the MQTT protocol. To avoid repetition, details are not described here. Specifically, Figure 4 The device 400 shown can perform the message communication method embodiment based on the MQTT protocol described above, and the foregoing and other operations and / or functions of each module in the device 400 are respectively for realizing the corresponding flow in the message communication method embodiment based on the MQTT protocol. For the sake of brevity, details are not described here.

[0076] The device 400 of the embodiment of the present application is described above from the perspective of functional modules in combination with the accompanying drawings. It should be understood that the functional modules can be realized in the form of hardware, or in the form of instructions of software, or in the form of a combination of hardware and software modules.

[0077] The above is merely specific implementation of the present application, but the protection scope of the present application is not limited to this. Any person skilled in the art can easily think of changes or replacements within the technical scope disclosed in the present application, which should be covered in the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.

Claims

1. A message transmission method based on MQTT protocol, characterized in that, The method comprises the following steps: a message sender sends a target message to an MQTT server, the target message comprising a message topic and a message content, the message topic carrying a message type and a client identifier of a target receiver; the MQTT server sends the target message to the target receiver according to the message type and the client identifier of the target receiver; the target receiver creates a request to subscribe to a topic as a subscriber, the topic carrying a subscription type, the message type and a client identifier of the message sender; the MQTT server determines whether the subscription type is an exclusive subscription, the exclusive subscription being used to represent that only one subscriber is allowed to exist at the same time for a topic; when the subscription type is the exclusive subscription, the MQTT server establishes a mutual exclusion lock on the topic to reject a subscription request of a client other than the target receiver; the message sender establishes a mutual exclusion lock according to the topic as a publisher and takes the topic as a publishing topic of the message sender, so that the topic is only subscribed by the target receiver.

2. The method of claim 1, wherein, Before the message sender sends the target message to the MQTT server, the method further comprises the following steps: the message sender constructs a two-level topic and a three-level topic according to the message type and the client identifier of the target receiver; the two-level topic is the message type, and the three-level topic is the client identifier of the target receiver; the two-level topic and the three-level topic are taken as the message topic, and the message content is taken as a load to create the target message.

3. The method of claim 2, wherein, The MQTT server sends the target message to the target receiver according to the message type and the client identifier of the target receiver, which comprises the following steps: the MQTT server parses the target message to obtain the two-level topic and the three-level topic; it is determined whether the two-level topic is a preset message type; if the two-level topic is the preset message type, the client identifier of the target receiver is extracted from the three-level topic, and the target message is sent to the target receiver; or if the two-level topic is not the preset message type, the target message is ignored.

4. The method of claim 3, wherein, The MQTT server extracts the client identifier of the target receiver from the three-level topic and sends the target message to the target receiver, which comprises the following steps: the MQTT server determines whether the target receiver is online according to the client identifier of the target receiver in a client connection table; if the target receiver is online, the target message is sent to the target receiver and a result of successful forwarding is returned; or if the target receiver is not online, the target message is stored in an offline message table, and a result of successful storage is returned; the offline message table stores a client identifier of each offline client and a corresponding unread message list.

5. The method of claim 1, wherein, The method further comprises the following steps: the message sender and the target receiver perform one-to-one message transmission based on the topic and perform confirmation or retransmission according to a preset quality of service level; after the message sender or the target receiver ends the communication, the message sender or the target receiver releases the mutual exclusion lock on the topic and sends a disconnection request to the MQTT server. The MQTT server releases the mutual exclusion lock on the topic after receiving the disconnection request.

6. The method of claim 1, wherein, The target receiver creates a request to subscribe to a topic as a subscriber, and the topic carries a subscription type, a message type, and a client identifier of a message sender. The MQTT server checks whether the topic requested to be subscribed by the target receiver has been subscribed by other clients. If the topic has been subscribed by other clients, the subscription request of the target receiver is rejected; or If the topic has not been subscribed by other clients, the subscription request of the target receiver is allowed, and the client of the target receiver is associated with the topic, so that there is only one subscriber for each topic. The MQTT server checks whether the topic requested to be subscribed by the target receiver has been unsubscribed. If the topic has been unsubscribed by the target receiver, the association between the client of the target receiver and the topic is released, and other clients are allowed to subscribe to the topic.

7. The method of claim 6, wherein, The method further comprises: The MQTT server releases the association between the client of the target receiver and the topic after receiving the message of the target receiver for unsubscribing, so as to allow other clients to subscribe to the topic.

8. A messaging apparatus based on MQTT protocol, characterized in that, Comprise: A message sending module, configured to send a target message to an MQTT server, the target message comprising a message topic and message content, and the message topic carrying a message type and a client identifier of a target receiving module; An MQTT server, configured to send the target message to the target receiving module according to the message type and the client identifier of the target receiving module; A target receiving module, configured to create a request to subscribe to a topic as a subscriber, and the topic carrying a subscription type, the message type, and a client identifier of a message sending module; The MQTT server is further configured to determine whether the subscription type is an exclusive subscription, and the exclusive subscription is used to represent that there is only one subscriber allowed for a topic at the same time. When the subscription type is the exclusive subscription, the MQTT server is configured to establish a mutual exclusion lock on the topic, so as to reject the subscription request of a client other than the target receiving module; The message sending module is configured to establish a mutual exclusion lock according to the topic as a publisher, and use the topic as a publishing topic of the message sending module, so that the topic is only subscribed by the target receiving module.

9. The apparatus of claim 8, wherein, The MQTT server comprises: A configuration module, configured to configure whether to start the exclusive subscription in a configuration file; When the exclusive subscription is started, the checking module is configured to check whether the topic requested to be subscribed by the target receiving module has been subscribed by other clients, or check whether the topic requested to be subscribed by the target receiving module has been unsubscribed; A subscription module, configured to allow the subscription request of the target receiving module, and associate the client of the target receiving module with the topic; and the subscription module is further configured to reject the subscription request of a client other than the target receiving module. The cancel subscription module is configured to, after receiving the message of the target receiving module canceling the subscription, disassociate the client of the target receiving module from the topic to allow other clients to subscribe to the topic.

10. The apparatus of claim 9, wherein, The MQTT server further comprises a mutex management module configured to establish or release a mutex on the topic according to a subscription message or a cancel subscription message, so that only one message sender and one target receiver are allowed to exist for each topic at the same time.

Citation Information

Patent Citations

  • Message receiving method and device based on MQTT protocol and storage medium

    CN110266810A

  • Distribution type information issuing / ordering system

    CN1908970A