MQTT has some terms that make it easy to remember, what does what.
There are two types of players in MQTT, a broker and a client. Compare it with REST we have a 'server' and then 'clients' that connect, the naming is different because the 'server' in MQTT is also managing data between clients. But, it functions in almost-the-same way
There’s only one broker, but there can be multiple clients.
- The broker is considered the ‘reliable’ one in the relationship - even if the connection goes out, the broker is expected to be available whenever the transport is working.
- The broker gets all the published messages from the clients and stores them in a database or memory.
- The broker then delivers the messages to the subscriber clients.
In general, the MQTT broker is not what we spend a lot of time engineering as there are many great free broker softwares (we're partial to Mosquitto), as well as dozens of broker services you can pay for that will do all the management for you. For example, Microsoft Azure, AWS IoT, even our very own adafruit.io
The part you will be most involved in is the client - that’s part of your application, whether it be a sensor, robot, or... toaster!
Here's an example showing the way brokers and clients interact
Let's say you have your internet of toaster at home (it has a WiFi chip, and is on your home network). And you have a geotracker in your car (a cellular+gps connection).
There's 2 ways you could have the two devices 'talk' to each other
- Have one of the devices run as a 'server' with an IP address, so that the other sensor can connect to it at any time
- Have a third 'server' somewhere, both toaster and car connect to that computer server and the computer sends messages back and forth.
Option #1 is in a sense, the 'least expensive' because no extra computer is needed. However, it's crazy difficult to pull off because the toaster or car have to be constantly waiting for a connection. And the other device needs to know the IP address of the listener. And then, what happens if a third device is involved?
Thus, we go with option #2 - the server that handles the messages? That's the MQTT Broker and the car and toaster are both MQTT Clients. You can now easily add more clients to add or monitor data, such as your mobile app (CyberToastApp, now available at the iTunes store?)