MQTT Get Started — IoT

Basic but important things you should know before using MQTT

Shaowen Zhu
3 min readMar 5, 2022

You will learn

  • What is MQTT broker and MQTT clients
  • How to config mosquitto
  • Difference between 0.0.0.0 and 127.0.0.1

1. MQTT broker and MQTT clients

MQTT broker is a server, like web server, which is responsible for message forwarding. I want to say there is a MQTT broker running on your PC like your computer or Raspberry Pi or whatever, and there are many clients which running on your phone or IoT like a program you wrote running on an esp8266. These clients connect the MQTT broker running on your PC and subscribe a topic or publish some messages to a specialized topic. And the MQTT broker just forward these messages to the other clients which have subscribed to this topic. What I want to say is that The MQTT connection is always between one client and the broker. Clients never connect to each other directly.

1.1. MQTT broker

There are many companies have implemented this server — MQTT broker.

For example,

Eclipse Mosquitto is an open source message broker.

RabbitMQ is an open source message broker software.

I run Mosquitto on my PC as a MQTT server to communicate with many clients like my flutter APP and some programs running on the esp8266.

If you want to run Mosquitto on your PC, you can download and install it.

Install first and start the service

1.2. MQTT client

There also are many companies have implemented the MQTT client with UI. Like MQTT.fx, MQTT Explorer. But don’t feel confused, you probably wanna say MQTT client is a program running on esp8266 or an APP. YES! But a MQTT client can be any device that can connect to the internet. Like Arduino UNO Wi-Fi Rev2 board.

2. Config mosquitto

After installing mosquitto, you will find a new folder called mosquitto, which location depends on your configuration while you were installing it.

Firstly, you need to add the environment variable, so that you can use mosquitto everywhere.

Secondly, you need to do some configurations in the mosquitto.conf which under the mosquitto folder. After modifying this file, you need to restart the mosquitto service.

If you want to find more information, please click this and this.

There are two places in mosquitto.conf that I want you to modify:

First place
Second place and Restart mosquitto
Any “ESTABLISHED” socket means that there is a connection currently made there.

Any “ESTABLISHED” socket means that there is a connection currently made there.

Any “LISTEN” means that the socket is waiting for a connection.

3. Difference between 0.0.0.0 and 127.0.0.1

Both of these IP addresses are used to set on the server, not the client. I mean, your server can listen either 0.0.0.0:port or 127.0.0.1:port. It's not a good idea to let your client establish a connection with an IP address 0.0.0.0 or 127.0.0.1.

127.0.0.1 is the loopback address (also known as localhost).

In the context of servers, 0.0.0.0 means “all IPv4 addresses on the local machine”. If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs. — superuser

--

--