MQTT WebSocket

What you will learn here about MQTT Websocket:

  • MQTT Websocket on Windows
  • MQTT Websocket on Google Cloud VM instance
  • MQTT Websocket on linux or raspberry pi

MQTT WebSocket allows you to exchange data between MQTT clients. Here we will see how to implement MQTT WebSocket on Windows, how to implement MQTT WebSocket on Google Cloud instance, how to implement MQTT WebSocket on Linux or raspberry pi.

MQTT WebSockets on Windows

Steps for implementing MQTT WebSockets on windows is given below:
1)In order to implement a MQTT WebSockets on windows, you need a MQTT broker. If you do not know how to install MQTT broker on windows please click on the following link.
Installing MQTT broker on windows

2)Now please go to your mosquitto installed folder. In my system, the mosquitto installed path is given below.
C:/Program Files (x86)/mosquitto




3)Now open the mosquitto.conf file with any editor and add following 3 lines which is shown below. Where 9001 is websocket port and 1883 is MQTT port.
port 1883
listener 9001
protocol websockets

4)Once you add the above three lines in mosquitto.conf please save the changes.

5) Now you need to open port 1883 and 9001 on windows system. Please click the below link to know, how to open port on windows.
how to open port in windows

6)Once you follow the above steps you need to restart the mosquitto or MQTT broker. Please follow the following steps to restart the mosquitto or MQTT broker on windows.

A)Open Command Prompt as Administrator. Steps are given below.
Search command Prompt in your computer -> Right click on command prompt -> Run as Administrator

B)Stop the MQTT service. Use following command to stop MQTT service.

net stop mosquitto

C)Now Start MQTT service. Use following command to stop MQTT service.

net start mosquitto

This how we install MQTT WebSockets on windows.

Video link – MQTT WebSockets

MQTT WebSockets on Google Cloud VM Instance

Steps for implementing MQTT WebSockets on Google Cloud VM Instance is given below:

1)In order to implement MQTT WebSockets on Google Cloud VM Instance, you need a MQTT broker on your VM instance. If you don’t know how to create a VM instance on google cloud, please click on the below link. If you don’t know how to install mqtt broker VM on google cloud, please click on the below link

Creating vm instance on google cloud

Installing mqtt broker on google cloud

2)Assuming you have a VM instance and you have installed MQTT broker on it using above links. Now open the SSH terminal of your VM instance by clicking on SSH of your VM instance.

3)Now navigate to the mosquitto folder using the following command.

cd /etc/mosquitto

4)Now execute the following command to edit mosquitto.conf file which is shown below.

sudo nano mosquitto.conf

5)Now please add the following three lines in mosquitto.conf file which is shown below.
port 1883
listener 9001
protocol websocket

6)Once you add the above three lines, now Press Ctrl + X and Type Y and hit enter to save your changes.

7)Once you save the changes you need to open port 1883 and 9001 on google cloud. So please click on the below link to how to open on google cloud.

Opening port on Google Cloud

8)Now you need to restart the mosquitto service. So in order restart mosquitto on google cloud vm instance, please click on SSH of your respective VM instance.

9)Execute the following command to stop the mosquitto service which is shown below.

sudo service mosquitto stop

10)Execute the following command to start the mosquitto service which is shown below.

sudo service mosquitto start

This is how we install MQTT websockets on the Google cloud VM instance.




MQTT WebSockets over Linux or raspberry pi

Please follow the following steps for implementing MQTT websockets over linux or raspberry pi.

1)In order to implement websocket on linux or raspberry pi , you need to install MQTT broker on linux or raspberry pi. If you don’t know how to install MQTT broker on linux or raspberry pi, please click on the below link.

Installing MQTT Broker on Linux

Installing MQTT Broker on Raspberry Pi

2)Assuming you have installed MQTT broker on linux or raspberry pi.

3)Now open the linux or raspberry terminal.

4)Execute the following command to navigate in the mosquitto folder which is shown below.

cd /etc/mosquitto

5)Execute the following command to edit mosquitto.conf file which is shown below.

sudo nano mosquitto.conf

6)Now add following 3 lines in mosquitto.conf file which is shown below.
port 1883
listener 9001
protocol websockets

7)Once you above 3 lines please press Ctrl + X and type y and hit enter to save the changes.

8)Now you need to restart the mosquitto or MQTT broker. Execute the following command to restart mosquitto or MQTT broker which is shown below.

sudo service mosquitto stop
sudo service mosquitto start

9)Now you need to port 1883 and 9001 on Linux or raspeberry pi. Please follow the following steps to open port 1883 and 9001 on Linux or raspeberry pi.
A)Open the linux or raspberry terminal

B)Execute the following command to install Uncomplicated Firewall i.e ufw

sudo apt install ufw

C)Execute the following command to open port 1883

sudo ufw allow 1883

D)Execute the following command to open port 9001

sudo ufw allow 9001

C)Execute the following command to enable firewall

sudo ufw enable




COMMON PROBLEMS / DOUBTS

1)What is default port for Websocket

Default port for WebSocket is 9001.

2)Can I change Websocket port

Yes, you can change the WebSocket port. In order to change the WebSocket port, please change the listener port and then restart the mosquitto or MQTT Broker

3)MQTT WebSocket and Thingsboard is not working together on the same system

MQTT WebSocket and Thingsboard are not working on the same system because thingsboard also uses WebSocket port 9001. So if you want MQTT WebSocket and Thingsboard to be run on the same system then please install MQTT WebSocket on some other port which is free on your system.

You may also like...

Leave a Reply