Create Your Own Mqtt Broker. Part 1

MQTT from IBM it is considered the machine-to-machine connectivity protocol. It is widely adopted, so lots of IoT solutions use it as the protocol to interconnect devices.

My next personal project will be the creation of a MQTT broker from scratch. I want to document the development process here, trying to write down all the problems I meet and the solutions I find to fix those problems.

But today I just have time to simply introduce few important concepts, before starting with the design and development of the broker.

Publish/Subscribe pattern

The way MQTT works is based in the publish/subscribe protocol, where two different type of actors take part of that process:

Initially the subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.

In MQTT, those classes are called topics.

Topics

The broker is the main responsible to keep the track of the subscribers (usually devices) that are subscribed to any topic.

It filters and recieves messages from publishers, and then it decides after which subscribers need to be notified.

To make it possible, the MQTT broker filter the message by means of the topic, a string that contains one or more topic levels. An example of a topic can be:

/house/kitchen/fridge/temperature

The string use slashes to separete each level of the topic, they are case-sensitive and each level must contain at least one character.

In MQTT, devices can publish and susbcribe to any level of the topic, although it is recommended to use specific topics instead of general ones.

Trie

Introduced on 1959 by Rene de la Briandais and Edward Fredking, the trie is a tree-type data structure that allows the easy recover of information (its name comes from the word reTRIEval).

The information stored in a trie is a set of keys, where a key is a sequence of symbols belonging to an alphabet. The keys are stored in the tree leaves and the internal nodes are gateways to guide the search.

alt trie

The main advantages of the tries, compared with other tree-type data structures like the binary search tree (BST) are:

This structure is the one I will probably use to store the list of topics by the MQTT broker. In addition, it will have to support the option of linking those topics with their subscribers.