Environment: Linux.
Requirements: Install the Mosquitto service and enable Mosquitto's Websockets service.
Installation package: Mosquitto1.6.9, libwebsockets
Note: 1. Install the libwebsockets service first, and ensure that the service can be started and used normally.
2. Make sure that the libwebsockets service is normal, and then configure and install the mosquitto service.
1. Install libwebsockets
1.1 Install libwebsockets dependencies
For the installation of dependencies, pay attention to the indispensable ones. If there is a yum warehouse, use yum to install it, or apt and other warehouses.
yum -y install openssl openssl-devel cmake
1.2 Install libwebsockets
Here you can choose to directly git clone to decompress locally, the address is: https://github.com/warmcat/libwebsockets
For the convenience of downloading, attach the network disk address of libwebsockets uploaded by other users:
libwebsockets.tar.gz - Blue Cloud
1.2.1 Decompression installation
sudo tar -zxvf libwebsockets.tar.gz -C /usr/local/libwebsockets
cd /usr/local/libwebsockets
mkdir build
cd build/
cmake ..
make && make install
If no error is reported, the example class file will be generated in the build/bin directory.
1.2.2. Secondary compilation :
Enter the source directory of the example: libwebsockets/minimal-examples-lowlevel/ws-server/minimal-ws-server
Compile and start the service for testing.
cd /usr/local/libwebsockets/minimal-examples-lowlevel/ws-server/minimal-ws-server
cmake .
make
./lws-minimal-ws-server
At this time, you can pass, add port 7681 to the local ip, visit the web page, and check the service startup status.
That is, if the local IP address is 192.168.1.1, you can access 192.168.1.1:7681 or 127.0.0.1:7681 (local access).
If the installation is successful: the webpage should normally display the libwebsockets page.
Displaying the libwebsockets logo page means that the compilation and installation are successful, otherwise libwebsockets will be unavailable.
otherwise,
You need to check whether the above steps report an error during the compilation process and execute again.
2. Install Mosquitto
2.1 Installation dependencies & decompression
For the installation of dependencies, pay attention to the indispensable ones. If there is a yum warehouse, use yum to install it, or apt and other warehouses.
For the Mosquitto version, I choose 1.6.9 here. The configuration after the new version 2.0 may be different in configuration, so that the websockets service cannot be opened.
The network disk address of the uploaded Mosquitto-1.6.9 is attached:
mosquitto-1.6.9.tar.gz - Blue Cloud
yum -y install gcc gcc-c++ c-ares-devel uuid-devel libuuid-devel
sudo tar -zxvf mosquitto-1.6.9.tar.gz -C /usr/local/
2.2 Edit the config.mk file
cd /usr/local/mosquitto-1.6.9
vi config.mk
Modify the configuration to enable the websockets service , on line 68, WITH_WEBSOCKETS:=no, change it to yes
Save changes and exit.
67 # Build with websockets support on the broker.
68 WITH_WEBSOCKETS:=yes
69
2.3 Executing the installation
Notice! The current path is still /usr/local/mosquitto-1.6.9
adduser mosquitto
make && make install
2.4 Configure the mosquitto.conf file
At this point the path is /etc/mosquitto/
cd /etc/mosquitto/
cp mosquitto.conf.example mosquitto.conf
vi mosquitto.conf
The modified position is the part after line 201 (Default listener) , mainly to set the default port port and websockets port
Here, the configuration is added in lines 211~213, the default port 1883 and port 9001 are added and designated as websockets. After the modification is completed, save the modification and exit.
200 # =================================================================
201 # Default listener
202 # =================================================================
203
204 # IP address/hostname to bind the default listener to. If not
205 # given, the default listener will not be bound to a specific
206 # address and so will be accessible to all network interfaces.
207 # bind_address ip-address/host name
208 #bind_address
209
210 # Port to use for the default listener.
211 port 1883
212 listener 9001
213 protocol websockets
214 # Bind the listener to a specific interface. This is similar to
215 # bind_address above but is useful when an interface has multiple addresses or
216 # the address may change. It is valid to use this with the bind_address option,
217 # but take care that the interface you are binding to contains the address you
218 # are binding to, otherwise you will not be able to connect.
219 # Example: bind_interface eth0
220 #bind_interface
2.5 Running Tests
mosquitto -v -c /etc/mosquitto/mosquitto.conf
If there are no special circumstances, the mosquitto service will start normally, and ports 1883 and 9001 will be opened according to the conf file configured in the specified path.
2.6 About the error handling encountered when the Mosquitto service is started
When running the mosquitto command and starting the mosquitto service, you may encounter an error, which may cause it to fail to run normally.
Example 1: Could not find libwebsockets.so.xx.
mosquitto: error while loading shared libraries: libwebsockets.so.19: cannot open shared object file: No such file or directory
Reason: After installing the service on some hosts, the soft connection is not established.
Solution: establish a soft connection.
sudo ln -s /usr/local/lib/libwebsocket.so.xx /usr/lib/libwebsocket.so.xx
ldconfig
Example 2: Invalid user 'mosquitto'.
Error: Invalid user 'mosquitto'.
Solution: Create a new user.
add user mosquitto
Example 3: Dependency problem.
error: ‘struct mosquitto’ has no member named ‘ssl’mosq->ssl = (SSL *)in
Solution: This kind of problem is that the dependencies have not been installed in advance, and the dependencies need to be installed in advance, such as openssl-devel.
Example 4: websockets are not available.
Error: Websockets support not available.
Cause: Caused by the order of service installation.
Solution: After installing libwebsockets correctly, configure the config.mk file during the mosquitto installation process to enable the installation of the websockets service.