CN116846959A - Message receiving and transmitting method and device based on MQTT protocol - Google Patents

Message receiving and transmitting method and device based on MQTT protocol Download PDF

Info

Publication number
CN116846959A
CN116846959A CN202310834349.4A CN202310834349A CN116846959A CN 116846959 A CN116846959 A CN 116846959A CN 202310834349 A CN202310834349 A CN 202310834349A CN 116846959 A CN116846959 A CN 116846959A
Authority
CN
China
Prior art keywords
message
target
theme
subscription
topic
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202310834349.4A
Other languages
Chinese (zh)
Inventor
刘瑞强
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Neusoft Ruichi Automotive Technology Dalian Co ltd
Original Assignee
Neusoft Ruichi Automotive Technology Dalian Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Neusoft Ruichi Automotive Technology Dalian Co ltd filed Critical Neusoft Ruichi Automotive Technology Dalian Co ltd
Priority to CN202310834349.4A priority Critical patent/CN116846959A/en
Publication of CN116846959A publication Critical patent/CN116846959A/en
Pending legal-status Critical Current

Links

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/50Network services
    • H04L67/51Discovery or management thereof, e.g. service location protocol [SLP] or web services
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/50Network services
    • H04L67/60Scheduling or organising the servicing of application requests, e.g. requests for application data transmissions using the analysis and optimisation of the required network resources

Landscapes

  • Engineering & Computer Science (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Information Transfer Between Computers (AREA)

Abstract

The application provides a message receiving and transmitting method and device based on an MQTT protocol, wherein the method comprises the following steps: the message sender sends a target message to the MQTT server; the MQTT server sends the target message to the target receiver according to the message type and the client identification of the target receiver; the target receiver serves as a subscriber to create a theme requesting subscription; the MQTT server judges whether the subscription type is an exclusive subscription or not, wherein the exclusive subscription is used for representing that only one subscriber is allowed to exist at the same time; when the subscription type is exclusive subscription, the MQTT server establishes a mutual exclusion lock on a theme so as to reject subscription requests of clients except a target receiver; the message sender serves as a publisher to establish a mutual exclusion lock according to the topic, and the topic serves as a self-published topic, so that the topic is subscribed only by the target receiver. To improve the efficiency and quality of data transmission.

Description

Message receiving and transmitting method and device based on MQTT protocol
Technical Field
The embodiment of the application relates to the technical field of information transmission, in particular to a message receiving and transmitting method and device based on an MQTT protocol.
Background
The MQTT (Message Queuing Telemetry Transport) is a lightweight communication protocol based on a publish/subscribe (publich/substricte) mode, and has wider application in the aspects of Internet of things, small-sized equipment, mobile application and the like.
However, current data publish and subscribe mechanisms have problems such as data redundancy, data inconsistencies, data collisions, and the like. For example, when multiple subscribers subscribe to the same topic at the same time, network resources may be wasted and congested, or data may be out of sync or inconsistent among different subscribers. In addition, when multiple publishers publish the same topic at the same time, a conflict or incompatibility of data between different publishers may occur.
Disclosure of Invention
The application provides a message receiving and sending method and device based on an MQTT protocol, which can realize one-to-one message receiving and sending, can realize mutually exclusive subscription to a theme, and improves the efficiency and quality of data transmission.
In a first aspect, a messaging method based on an MQTT protocol is provided, including: the message sender sends a target message to an MQTT server, wherein the target message comprises a message theme and a message content, and the message theme carries 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 serves as a subscriber to create a theme requesting subscription, wherein the theme carries a subscription type, the message type and a client identifier of a message sender; the MQTT server judges whether the subscription type is an exclusive subscription or not, wherein the exclusive subscription is used for representing that only one subscriber is allowed to exist at the same time on one topic; when the subscription type is exclusive subscription, the MQTT server establishes a mutual exclusion lock on the topic so as to reject subscription requests of clients except the target receiver; the message sender serves as a publisher to establish a mutual exclusion lock according to the theme, and the theme serves as a self-publishing theme, so that the theme is subscribed only by the target receiver.
Preferably, before the message sender sends the target message to the MQTT server, the method further comprises:
the message sender constructs a secondary theme and a tertiary theme according to the message type and the client identification of the target receiver; the secondary theme is a message type, and the tertiary theme is a client identifier of a target receiver; and creating the target message by taking the secondary theme and the tertiary theme as the message theme and taking the message content as the load.
Preferably, the MQTT server transmits the target message to the target receiver according to the message type and the client identifier of the target receiver, including: the MQTT server analyzes the target message to obtain the secondary theme and the tertiary theme; judging whether the secondary theme is of a preset message type or not; if the secondary theme is of a preset message type, extracting a client identifier of the target receiver from the tertiary theme, and sending the target message to the target receiver; or if the secondary topic is not the preset message type, ignoring the target message.
Preferably, the extracting the client identifier of the target receiver from the tertiary theme and sending the target message to the target receiver includes: the MQTT server searches whether the target receiver is online or not in a client connection table according to the client identification of the target receiver; if the target receiver is online, the target message is sent to the target receiver and a successful forwarding result is returned; or if the target receiver is not online, storing the target message in an offline message table, and returning a successful storage result; the offline message table stores a client identifier of each offline client and a corresponding unread message list.
Preferably, the method further comprises: the message sender and the target receiver perform one-to-one message receiving and transmitting based on the subject, and confirm or retransmit according to a preset service quality level; after the message sender or the target receiver finishes communication, releasing the mutual exclusion lock on the theme and sending a disconnection request to the MQTT server; and after receiving the disconnection request, the MQTT server releases the mutual exclusion lock on the theme.
Preferably, the target receiver creates a topic requesting subscription as a subscriber, where the topic carries a subscription type, the message type, and a client identifier of a message sender, and then further includes: the MQTT server checks whether the topic requested to be subscribed by the target receiver is subscribed by other clients; if the topic is subscribed by other clients, rejecting the subscription request of the target receiver; or if the topic is not subscribed by other clients, allowing the subscription request of the target receiver, and associating the client of the target receiver 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 is already unsubscribed; if the topic has been unsubscribed by the target recipient, disassociating the target recipient from the topic and allowing other clients to subscribe to the topic.
Preferably, the method further comprises: and after receiving the message that the target receiver unsubscribes, the MQTT server releases the association between the client of the target receiver and the theme so as to allow other clients to subscribe to the theme.
In a second aspect, there is provided a messaging device based on MQTT protocol, comprising:
the message sending module is used for sending a target message to the MQTT server, wherein the target message comprises a message theme and a message content, and the message theme carries a message type and a client identifier of the target receiving module; the MQTT server is used for sending the target message to the target receiving module according to the message type and the client identifier of the target receiving module; the target receiving module is used for creating a theme requesting subscription as a subscriber, wherein the theme carries a subscription type, a message type and a client identifier of the message sending module; the MQTT server is also used for judging whether the subscription type is an exclusive subscription or not, and the exclusive subscription is used for representing that only one subscriber is allowed to exist at the same time on one topic; when the subscription type is exclusive subscription, the MQTT server is used for establishing a mutual exclusion lock on the theme so as to reject subscription requests of clients outside the target receiving module; the message sending module is used as a publisher to establish a mutual exclusion lock according to the topic, and the subscription topic is used as a self publishing topic, so that the topic is subscribed by the target receiving module only.
Preferably, the MQTT server includes: the configuration module is used for configuring whether the exclusive subscription is opened or not in the configuration file; the checking module is used for checking whether the subject of the target receiving module requesting subscription is subscribed by other clients or not when the exclusive subscription is opened; or checking whether the subject of the target receiving module requesting subscription has been unsubscribed; a subscription module, configured to allow a subscription request of the target receiving module, and associate a client of the target receiving module with the topic; the subscription module is also used for rejecting the subscription request of the client outside the target receiving module; and the unsubscribing module is used for removing the association between the client side of the target receiving module and the theme after receiving the message of unsubscribing by the target receiving module so as to allow other client sides to subscribe to the theme.
Preferably, the MQTT server further comprises: and the mutual exclusion lock management module is used for establishing or releasing a mutual exclusion lock on the topics according to the subscription message or the unsubscribe message, so that each topic is only allowed to exist for one message sender and one target receiver at the same time.
According to the technical scheme provided by the application, a message sender firstly sends a target message to an MQTT server; the MQTT server sends the target message to the target receiver according to the message type and the client identifier of the target receiver, so that a one-to-one message receiving and sending mode can be realized, the theme or subscription relation is not required to be agreed in advance, the network overhead and the push delay are reduced, and the efficiency and the reliability of message transmission are improved; then the target receiver is used as a subscriber to create a theme requesting subscription; the MQTT server judges whether the subscription type is an exclusive subscription or not, wherein the exclusive subscription is used for representing that only one subscriber is allowed to exist at the same time; when the subscription type is exclusive subscription, the MQTT server establishes a mutual exclusion lock on a theme so as to reject subscription requests of clients except a target receiver; the message sender serves as a publisher to establish a mutual exclusion lock according to the topic, and the topic serves as a self-published topic, so that the topic is subscribed only by the target receiver. By checking whether the topic which is requested to be subscribed by a certain client is subscribed by other clients, the function of mutually exclusive subscription of the topic is realized, and the problems of data redundancy, inconsistent data, data conflict and the like caused by that a plurality of subscribers subscribe to the same topic at the same time are avoided.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for the description of the embodiments will be briefly described below, and it is apparent that the drawings in the following description are only some embodiments of the present application, and other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is an application scenario diagram provided in an embodiment of the present application;
FIG. 2 is a flow chart of a message sending and receiving method based on the MQTT protocol according to an embodiment of the present application;
FIG. 3 is a flow chart of another messaging method based on the MQTT protocol provided by an embodiment of the present application;
FIG. 4 is a schematic diagram of a message transceiver device based on the MQTT protocol according to an embodiment of the present application;
fig. 5 is a schematic block diagram of the MQTT server in fig. 4.
Detailed Description
The following description of the embodiments of the present application will be made clearly and completely with reference to the accompanying drawings, in which it is apparent that the embodiments described are only some embodiments of the present application, but not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
It should be noted that the terms "first," "second," and the like in the description and the claims of the present application and the above figures are used for distinguishing between similar objects and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used may be interchanged where appropriate such that the embodiments of the application described herein may be implemented in sequences other than those illustrated or otherwise described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or server that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed or inherent to such process, method, article, or apparatus, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
As described above, there are a large number of data publishing and subscribing scenes in the field of the internet of vehicles, such as vehicles acquiring road condition information, navigation information, entertainment information, etc., or vehicles publishing own status information, safety warning information, rescue information, etc. These scenarios require efficient management of matching relationships between data publishers and subscribers to achieve accurate delivery and consumption of data.
However, the data publish and subscribe mechanism has some problems such as data redundancy, data inconsistency, data collision, etc. For example, when multiple subscribers subscribe to the same topic at the same time, network resources may be wasted and congested, or data may be out of sync or inconsistent among different subscribers. In addition, when multiple publishers publish the same topic at the same time, a conflict or incompatibility of data between different publishers may occur.
In order to solve the technical problems, the application concept of the application is as follows: and then, based on a new data publishing and subscribing mechanism, mutually exclusive subscription (i.e. only one subscriber is allowed to exist in one topic at the same time) and P2P (peer to peer) communication are carried out on the topics so as to improve the efficiency and quality of data transmission.
It should be understood that the technical solution of the present application can be applied to the following scenarios, but is not limited to:
in some implementations, fig. 1 is an application scenario diagram provided by an embodiment of the present application, and as shown in fig. 1, the messaging system may include a first MQTT client 110, an MQTT server 120, and a second MQTT client 130. The first MQTT client 110, the second MQTT client 130 may establish a connection with the MQTT server 120 through a wired network or a wireless network.
Illustratively, the first MQTT client 110, the second MQTT client 130 may be, but are not limited to, a desktop computer, a notebook computer, a tablet computer, etc. The MQTT server 120 may be a terminal device or a server, but is not limited thereto. In one embodiment of the present application, the first MQTT client 110 may send the target message to the MQTT server 120 as a message sender, further, the MQTT server 120 may receive the target message sent by the first MQTT client 110 and forward the target message to the second MQTT client 130, and the second MQTT client 130 may receive the target message forwarded by the MQTT server 120 as a target receiver.
Furthermore, FIG. 1 illustrates a first MQTT client 110 and a second MQTT client 130, in fact, other numbers of clients could be included, as the application is not limited in this regard.
After the application scenario of the embodiment of the present application is introduced, the following details of the technical solution of the present application will be described:
fig. 2 is a flowchart of a messaging method based on MQTT protocol according to an embodiment of the present application, where the method may include the following steps:
s210: the message sender sends a target message to the MQTT server, wherein the target message comprises a message theme and a message content, and the message theme carries a message type and a client identification of a target receiver.
In this step, the message sender may send a target message to the MQTT server through the first MQTT client, where the target message is an MQTT message, and the MQTT message includes two parts including a Topic (Topic) and a payload (payload), and the message sender may construct a secondary Topic and a tertiary Topic according to the message type and a client identifier of the target receiver, where the secondary Topic is a message type, and exemplary, the secondary Topic may be "p2p"; the tertiary topic identifies the Client of the target recipient, and illustratively, the tertiary topic may identify "Client ID2" for the Client of the second MQTT Client, then create the target message with the secondary topic and the tertiary topic as message topics and the message content as loads.
S220: the MQTT server sends the target message to the target receiver according to the message type and the client identification of the target receiver.
In this embodiment, the target message may be sent to the target receiver through the second MQTT client, and the MQTT server may parse the target message to obtain a secondary topic and a tertiary topic, then determine whether the secondary topic is a preset message type, extract the client identifier of the target receiver from the tertiary topic if the secondary topic is the preset message type, and send the target message to the target receiver, and ignore the target message if the secondary topic is not the preset message type.
For example, the preset message type may be a preset "p2p" type, and the MQTT server determines whether the secondary topic is "p2p", and if so, sends the target message to the target receiver; if not, the message is ignored or processed in a publish/subscribe mode. After the target receiver is connected to the MQTT server through the second MQTT client, the P2P message from the message sender can be directly received from the MQTT server without subscribing any theme.
S230: the target receiver serves as a subscriber to create a topic requesting subscription, and the topic carries a subscription type, a message type and a client identification of a message sender.
In this step, after receiving the target message, the target receiver may reply a confirmation message to the MQTT server, and create a topic requesting subscription as the subscriber, where the topic carries a subscription type, a message type, and a client identifier of the message sender, and the topic requesting subscription may be: exclusive/p2p/Client ID1, where "exclusive" indicates that the subscription type is exclusive subscription, "p2p" indicates that the message type is a p2p message, and "Client ID1" indicates that the Client identity of the message sender is Client ID1.
S240: the MQTT server determines whether the subscription type is an exclusive subscription that characterizes one topic as only being allowed to exist for one subscriber at a time.
It will be appreciated that the subscription types may include a variety of, for example: a normal subscription for characterizing that one topic is allowed to exist for a plurality of subscribers at the same time, or an exclusive subscription for characterizing that one topic is allowed to exist for only one subscriber at the same time.
S250: when the subscription type is exclusive subscription, the MQTT server establishes a mutual exclusion lock on the topic to reject subscription requests of clients other than the target receiver.
S260: the message sender serves as a publisher to establish a mutual exclusion lock according to the topic, and the topic serves as a self-published topic, so that the topic is subscribed only by the target receiver.
In this embodiment, after receiving the acknowledgement message of the target receiver, the MQTT server may establish a mutual exclusive lock on the topic $ exclusive/p2p/Client ID1, and forward the acknowledgement message to the message sender, where the acknowledgement message is used to indicate that the target receiver has received the target message. After receiving the acknowledgement message, the message sender can also establish a mutual exclusive lock on the $exclusive/p2p/Client ID1 and use the topic as its own release topic, so that the message sender and the target receiver perform one-to-one message receiving and transmitting on the $exclusive/p2p/Client ID1, and perform acknowledgement or retransmission according to a preset or negotiated quality of service (QoS) level. Reliability guarantees in P2P communication are achieved so that each message can be delivered to a destination at different QoS levels.
Further, after the message sender or the target receiver finishes communication, the mutual exclusion lock can be released on the topic, and a disconnection request is sent to the MQTT server; after the MQTT server receives the disconnection request, the mutual exclusion lock can be released on the topic.
Further, extracting the client identifier of the target receiver from the three-level theme, and sending the target message to the target receiver, and further including:
s310: and the MQTT server searches whether the target receiver is online or not in the client connection table according to the client identification of the target receiver.
S320: if the target receiver is online, sending the target message to the target receiver and returning a successful forwarding result; or if the target receiver is not online, storing the target message in an offline message table, and returning a successful storage result; the offline message table stores a client identification for each offline client and a corresponding list of unread messages.
When the target receiver is on line, the MQTT server searches the client identification and the unread message list of the target receiver from the offline message list, and forwards all messages in the unread message list to the target receiver in sequence until the unread message list is empty.
In this embodiment, the message receiving and sending method of the P2P mode based on the MQTT protocol can implement a one-to-one message receiving and sending mode without specifying the topic or subscription relationship in advance, thereby reducing network overhead and push delay and improving the efficiency and reliability of message transmission. Meanwhile, the problems of high registration and subscription relation cost, high message forwarding and distribution overhead, message pushing delay loss and the like in the prior art of message sending and receiving based on a publish/subscribe mode are solved, and the performance and quality of the message sending and receiving are improved.
Fig. 3 is a flowchart of another messaging method based on MQTT protocol according to an embodiment of the present application. As shown in fig. 3, after step S230, further includes:
s270: the MQTT server checks whether the subject of the target receiver's request subscription has been subscribed to by other clients.
S280: if the topic is subscribed by other clients, rejecting the subscription request of the target receiver; or if the subscription topic is not subscribed by other clients, allowing the subscription request of the target receiver, and associating the client of the target receiver with the topic so that each topic has only one valid subscriber.
S290: after receiving the message that the target receiver unsubscribes, the MQTT server cancels the association between the client of the target receiver and the theme so as to allow other clients to subscribe to the theme.
In this embodiment, whether to open the exclusive subscription can be configured in the configuration file in advance through the MQTT server or the configuration module, so that whether to use the exclusive subscription function can be selected according to different internet of vehicles scenes and requirements, so as to increase the adaptability and expandability of the system. If the exclusive subscription is opened, the MQTT server or the checking module checks whether the topic requested to be subscribed by a certain client is subscribed by other clients, so that the function of mutually exclusive subscription of the topic is realized, and the problems of data redundancy, data inconsistency, data conflict and the like caused by that a plurality of subscribers subscribe to the same topic at the same time are avoided. For example, an exclusive subscription prefix may be provided for identifying topics for which an exclusive subscription is desired. The MQTT server or the dynamic theme generation module can generate a dynamic theme according to the client identification of the target receiver; for example, $ exclusive/t/1 may be used to represent topic t/1 for which an exclusive subscription is desired, thereby facilitating classification and filtering of topics by the system and reducing complexity and overhead of the system.
Further, the subscription request of a certain client can be allowed or refused through the MQTT server or the subscription module, and the client is associated with the theme when the subscription request of the certain client is allowed; the association of a certain client with a certain topic may be disassociated by the MQTT server or the unsubscribe module and other clients may be allowed to subscribe to the topic. The function of dynamically updating the topics is realized, and each topic is ensured to always have an up-to-date subscriber.
Further, the MQTT server or the control message processing module may generate or parse a control message according to the dynamic theme and interact with the MQTT server; the MQTT server or mutex lock management module may then establish or release the mutex lock on the dynamic theme based on the control message. Mutual exclusivity assurance in the P2P communication process is realized, so that each dynamic theme is only allowed to exist for one sender and one receiver at the same time.
It should be noted that, the configuration module, the inspection module, the dynamic theme generation module, the subscription module, the unsubscribe module, the control message processing module, the mutual exclusion lock management module, and the like in this embodiment may be integrated in another server, where the server may interact with the first MQTT client and the second MQTT client; in another embodiment, the above modules may also be provided in an MQTT server, which is not specifically limited in this embodiment.
Fig. 4 is a schematic diagram of a messaging device 400 according to an embodiment of the present application. As shown in fig. 4, the apparatus 400 includes:
a message sending module 410, configured to send a target message to the MQTT server 420, where the target message includes a message topic and a message content, and the message topic carries a message type and a client identifier of the target receiving module 430;
the MQTT server 420 is 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;
a target receiving module 430, configured to create, as a subscriber, a topic requesting subscription, where the topic carries a subscription type, the message type, and a client identifier of the message sending module 410;
the MQTT server 420 is further configured to determine whether the subscription type is an exclusive subscription, where the exclusive subscription is used to characterize that only one subscriber is allowed to exist at the same time for one topic;
when the subscription type is an exclusive subscription, the MQTT server 420 is configured to establish a mutex lock on the topic to reject subscription requests of clients other than the target receiving module 430;
the message sending module 410 is configured to establish a mutual exclusion lock as a publisher according to the topic, and take the topic as its own published topic, so that the topic is subscribed only by the target receiving module 430.
In some implementations, the MQTT server 420 further comprises:
a configuration module 4201 for configuring whether to open an exclusive subscription in a configuration file;
a checking module 4202 for checking, when an exclusive subscription is opened, whether the subject of the subscription requested by the target receiving module 430 has been subscribed by other clients; or checking whether the subject of the subscription requested by the target receiving module 430 has been unsubscribed;
a subscription module 4203 for allowing subscription requests of the target receiving module 430 and associating clients of the target receiving module 430 with the topics; the subscription module 4203 is further configured to reject subscription requests of clients other than the target receiving module 430;
the unsubscribe module 4204 is configured to, after receiving the unsubscribe message from the target receiving module 430, disassociate the client of the target receiving module 430 from the topic, so as to allow other clients to subscribe to the topic.
In some implementations, the MQTT server 420 further comprises:
the mutual exclusion lock management module 4205 is configured to set up or release mutual exclusion locks on topics according to subscription messages or unsubscribe messages, such that each topic is allowed to exist only for one message sender and one target receiver at a time.
It should be appreciated that the apparatus embodiments and the MQTT protocol-based messaging method embodiments may correspond to one another, and similar descriptions may refer to MQTT protocol-based messaging method embodiments. To avoid repetition, no further description is provided here. Specifically, the apparatus 400 shown in fig. 4 may perform the above-described messaging method embodiment based on the MQTT protocol, and the foregoing and other operations and/or functions of each module in the apparatus 400 are respectively for implementing the corresponding flows in the above-described messaging method based on the MQTT protocol, which are not described herein for brevity.
The apparatus 400 of the embodiment of the present application is described above in terms of functional modules with reference to the accompanying drawings. It should be understood that the functional module may be implemented in hardware, or may be implemented by instructions in software, or may be implemented by a combination of hardware and software modules.
The foregoing is merely illustrative of the present application, and the present application is not limited thereto, and any person skilled in the art will readily appreciate variations or alternatives within the scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.

Claims (10)

1. A messaging method based on MQTT protocol, comprising:
the message sender sends a target message to an MQTT server, wherein the target message comprises a message theme and a message content, and the message theme carries 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 serves as a subscriber to create a theme requesting subscription, wherein the theme carries a subscription type, the message type and a client identifier of a message sender;
the MQTT server judges whether the subscription type is an exclusive subscription or not, wherein the exclusive subscription is used for representing that only one subscriber is allowed to exist at the same time on one topic;
when the subscription type is exclusive subscription, the MQTT server establishes a mutual exclusion lock on the topic so as to reject subscription requests of clients except the target receiver;
the message sender serves as a publisher to establish a mutual exclusion lock according to the theme, and the theme serves as a self-publishing theme, so that the theme is subscribed only by the target receiver.
2. The method of claim 1, wherein before the message sender sends the target message to the MQTT server, further comprising:
the message sender constructs a secondary theme and a tertiary theme according to the message type and the client identification of the target receiver; the secondary theme is a message type, and the tertiary theme is a client identifier of a target receiver;
and creating the target message by taking the secondary theme and the tertiary theme as the message theme and taking the message content as the load.
3. The method of claim 2, wherein the MQTT server transmitting the target message to the target recipient based on the message type and the client identification of the target recipient, comprising:
the MQTT server analyzes the target message to obtain the secondary theme and the tertiary theme;
judging whether the secondary theme is of a preset message type or not;
if the secondary theme is of a preset message type, extracting a client identifier of the target receiver from the tertiary theme, and sending the target message to the target receiver; or alternatively
And if the secondary theme is not the preset message type, ignoring the target message.
4. The method of claim 3, wherein the extracting the client identification of the target recipient from the tertiary topic and sending the target message to the target recipient comprises:
the MQTT server searches whether the target receiver is online or not in a client connection table according to the client identification of the target receiver;
if the target receiver is online, the target message is sent to the target receiver and a successful forwarding result is returned; or alternatively
If the target receiver is not online, storing the target message in an offline message table, and returning a successful storage result; the offline message table stores a client identifier of each offline client and a corresponding unread message list.
5. The method according to claim 1, wherein the method further comprises:
the message sender and the target receiver perform one-to-one message receiving and transmitting based on the subject, and confirm or retransmit according to a preset service quality level;
after the message sender or the target receiver finishes communication, releasing the mutual exclusion lock on the theme and sending a disconnection request to the MQTT server;
and after receiving the disconnection request, the MQTT server releases the mutual exclusion lock on the theme.
6. The method of claim 1, wherein the target recipient creates a topic of the requested subscription as a subscriber, the topic carrying a subscription type, the message type, and a client identification of a message sender, further comprising:
the MQTT server checks whether the topic requested to be subscribed by the target receiver is subscribed by other clients;
if the topic is subscribed by other clients, rejecting the subscription request of the target receiver; or alternatively
If the topics are not subscribed by other clients, allowing a subscription request of the target receiver, and associating the clients of the target receiver with the topics so that each topic has only one subscriber;
the MQTT server checks whether the topic requested to be subscribed by the target receiver is already unsubscribed;
if the topic has been unsubscribed by the target recipient, disassociating the target recipient from the topic and allowing other clients to subscribe to the topic.
7. The method of claim 6, wherein the method further comprises:
and after receiving the message that the target receiver unsubscribes, the MQTT server releases the association between the client of the target receiver and the theme so as to allow other clients to subscribe to the theme.
8. A messaging apparatus based on MQTT protocol, comprising:
the message sending module is used for sending a target message to the MQTT server, wherein the target message comprises a message theme and a message content, and the message theme carries a message type and a client identifier of the target receiving module;
the MQTT server is used for sending the target message to the target receiving module according to the message type and the client identifier of the target receiving module;
the target receiving module is used for creating a theme requesting subscription as a subscriber, wherein the theme carries a subscription type, the message type and a client identifier of the message sending module;
the MQTT server is also used for judging whether the subscription type is an exclusive subscription or not, and the exclusive subscription is used for representing that only one subscriber is allowed to exist at the same time on one topic;
when the subscription type is exclusive subscription, the MQTT server is used for establishing a mutual exclusion lock on the theme so as to reject subscription requests of clients outside the target receiving module;
the message sending module is used as a publisher to establish a mutual exclusion lock according to the theme, and the theme is used as a self-publishing theme, so that the theme is subscribed by the target receiving module only.
9. The apparatus of claim 8, wherein the MQTT server comprises:
the configuration module is used for configuring whether the exclusive subscription is opened or not in the configuration file;
the checking module is used for checking whether the subject of the target receiving module requesting subscription is subscribed by other clients or not when the exclusive subscription is opened; or checking whether the subject of the target receiving module requesting subscription has been unsubscribed;
a subscription module, configured to allow a subscription request of the target receiving module, and associate a client of the target receiving module with the topic; the subscription module is also used for rejecting the subscription request of the client outside the target receiving module;
and the unsubscribing module is used for removing the association between the client side of the target receiving module and the theme after receiving the message of unsubscribing by the target receiving module so as to allow other client sides to subscribe to the theme.
10. The apparatus of claim 9, wherein the MQTT server further comprises: and the mutual exclusion lock management module is used for establishing or releasing a mutual exclusion lock on the topics according to the subscription message or the unsubscribe message, so that each topic is only allowed to exist for one message sender and one target receiver at the same time.
CN202310834349.4A 2023-07-07 2023-07-07 Message receiving and transmitting method and device based on MQTT protocol Pending CN116846959A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310834349.4A CN116846959A (en) 2023-07-07 2023-07-07 Message receiving and transmitting method and device based on MQTT protocol

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310834349.4A CN116846959A (en) 2023-07-07 2023-07-07 Message receiving and transmitting method and device based on MQTT protocol

Publications (1)

Publication Number Publication Date
CN116846959A true CN116846959A (en) 2023-10-03

Family

ID=88172283

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310834349.4A Pending CN116846959A (en) 2023-07-07 2023-07-07 Message receiving and transmitting method and device based on MQTT protocol

Country Status (1)

Country Link
CN (1) CN116846959A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117014850A (en) * 2023-10-07 2023-11-07 深圳市鲸视科技有限公司 Device searching method, system, device, computer device and storage medium
CN118075342A (en) * 2024-02-04 2024-05-24 湖南元智电集控科技有限公司 Multi-protocol distribution device based on star-shaped convergence point and application method thereof

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117014850A (en) * 2023-10-07 2023-11-07 深圳市鲸视科技有限公司 Device searching method, system, device, computer device and storage medium
CN117014850B (en) * 2023-10-07 2024-01-19 深圳市鲸视科技有限公司 Device searching method, system, device, computer device and storage medium
CN118075342A (en) * 2024-02-04 2024-05-24 湖南元智电集控科技有限公司 Multi-protocol distribution device based on star-shaped convergence point and application method thereof

Similar Documents

Publication Publication Date Title
US8447815B2 (en) System and method for instant message transmission in mobile communication terminal
CN116846959A (en) Message receiving and transmitting method and device based on MQTT protocol
US20110282957A1 (en) Method for processing deferred message
CN101257460B (en) Instantaneous message temporary cluster group conversational system and method for creating and transmitting instantaneous message
RU2398358C2 (en) Method and device for interaction between push-to-talk (poc) and instant messaging (im) system in cellular communication network
US20190116140A1 (en) Method of notification of the unavailability of a terminal
CN106210049B (en) Cluster communication method and system based on message queue
EP4373035A2 (en) Multicast service session operation method and apparatus, and communication device
CN110875914B (en) Method and device for transmitting messages based on shared session link
CN101754124A (en) Information transfer processing device, system and method
KR100617775B1 (en) Method for managing duplicated message notification in multimedia messaging service
WO2010041869A2 (en) Mobile communication system and method for the requesting of im positive delivery reports from individual group members
CN103457746A (en) Meeting data transmission method and device
US20120011260A1 (en) Method and system for transmitting large message mode converged IP messaging
CN101889426A (en) A method of reducing size of presence messages
CN115225706B (en) Data transmission method, device, vehicle and storage medium
CN101330664B (en) Method, system and apparatus for realizing multimedia business
EP2690833B1 (en) Media message sending method, device and system
CN108337215A (en) A kind of document transmission method and system, device, electronic equipment
CN101964957A (en) Orientating method fused with IP message and system
CN101860544A (en) Transmitting system and method of session initiation protocol message
US9451420B2 (en) Data processing method, in an ad hoc radio communication network, radio communication stations and corresponding computer programs
CN113454959A (en) Control plane network function, user plane network function, and packet processing method using the same
EP2640029A1 (en) System and method for transmitting multipage multimedia messages, session initiation protocol terminal and multimedia message proxy server
US20220182800A1 (en) Message transmission in a multi-terminal context

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination