# Secure Data Distribution Architecture in IoT Using MQTT
## 3 Distributed MQTT Architecture
### Discovery Service  
The discovery service is an HTTP service that provides real-time updates to all its passed interests of consumers between them. The service uses topic names as grouping criteria for different brokers such that all brokers managing publishers of a certain topic always receive updates about new subscriptions of that topic. In our running example, the interactions with the discovery service are as follows:  
1. \(A\) and \(B\) connect and authenticate to _discovery service_, where sockets are maintained at all times.
2. \(A\) advertises interest in publishing to topic \(T\) to the _discovery service_, which adds \(A\) to a group called \(T\).
3. \(B\) advertises interest in subscribing to topic \(T\) to the _discovery service_.
4. Upon receiving the subscription request the _discovery service_ pushes a notification of a new subscription to all the brokers in the group called \(T\), which only includes \(A\) at the moment.
5. \(A\) can decide independently if it wants to disseminate data to \(B\) or not.  
Figure 3: Running example illustration.  
There are two interesting observations about this scheme. First, connections are maintained with the discovery service at all times, which adds the following benefits: (a) More efficient because it reduces the amount of data sent per transmission because it cuts most of the application layer headers and (b) Enables real-time bidirectional communication with all brokers. The second interesting aspect is that the subscribers are informed of new publishers not the other way around, which is performed because of the following reasons: (a) The number of publishers is usually less than the number of subscribers in a given system, (b) List of publishers is usually more stable than the list of subscribers in a given system, (c) Most of the time, subscriptions come after the publications which means we can present a subscriber with all available publishers and gradually stream new publishers to existing subscribers and (d) Give the subscribers more privacy, in that the subscribers can choose which publisher to connect to and consume its services. It should be noted that all communication is performed via the opened sockets in JSON format to send different types of messages, as follows:  
1. New publisher advertisement(NPA): Sent to the discovery service by a new publisher that wants to publish data on topic \(T\). The message structure is as follows: { type: ''nepub'', address: ''broker address'', topic: ''T'' }
2. New subscriber advertisement(NSA): Sent to the discovery service by a new subscriber that wants to subscribe to the publication of topic \(T\). The message structure is as follows: { type: ''nevsub'', address: ''broker address'', topic: ''T'' } The server responds with a list of all existing publishers of topic \(T\). Then, it streams new publishers gradually using _NPA_ messages to that subscriber. The discovery service acts as a streaming server that keeps track of all publishers and subscribers in memory and streams new publishers to interested subscribers as soon as they come. Figure 4 illustrates a more generic process when there is more than one subscriber and more than one publisher.