Getting started with ReactJS and MQTT – IoT Dashboard

by Dec 15, 2022IoT Continuous Integration, MQTT, React JS

React is an open-source front-end JavaScript library for creating user interfaces or UI components. It is sometimes known as React.js or ReactJS. Facebook and a group of independent programmers and businesses collectively maintain it. Mobile or single-page applications can be built using React as a framework. React, however, primarily focuses on rendering data to the DOM. Therefore, to create React apps, extra frameworks for state management and routing are typically needed. Examples of these libraries include Redux and React Router, respectively. This post primarily explains how to build connect, subscribe, messaging, and unsubscribe between the client and MQTT broker using the React project.

Pre-requisites – Websockets

For this tutorial, ensure you have activated web sockets to connect to the broker using a web client.

WebSocket is one of the communication protocols which provides full duplex communication over a single TCP/IP connection. It uses HTTP as an initial connection establishment. The WebSocket enables communication from the web browser (client) to the server, in which you can send some data or real-time data to the client from the server or even bidirectional. At first, the client and the server interact with HTTP, then the connection upgrades to the WebSocket, providing full duplex communication, unlike HTTP.

HTTP Protocol always uses long polling, but Websockets overcomes this problem. Because the HTTP protocol always sends the data on request/response, and the WebSockets allows the server to send the data to the web browser or the client, even without any request from the browser. So, 2-way client-server communication is possible with WebSockets.

Follow this tutorial to activate the WebSockets on your Windows or Linux machine.

Step 1 Create a new react project.

Create a new react application, “mqtt-dashboard

 npx create-react-app mqtt-dashboard

Step 2 Install the MQTT.js client on your project.

MQTT.js is a client library for the MQTT protocol, written in JavaScript for node.js and the browser.

cd mqtt-dashboard
npm install mqtt

Step 3 Edit your src/App.js file

Provide all the information related to the Broker in the file. We will only subscribe to the broker from our client.

import React, { useState, Fragment } from "react";
import "./App.css";

var mqtt = require("mqtt");
var options = {
  protocol: "ws",
  username: "xxxxxxxxx",
  password: "xxxxxxxx",
  keepalive: 20,
  // clientId uniquely identifies client
  // choose any string you wish
  clientId: "mqttjs_" + Math.random().toString(16).substr(2, 8),
};
var client = mqtt.connect("mqtt://<your IP address>:<your websocket port>", options);

client.subscribe("publishtopic");
console.log("Client subscribed ");

function App() {
  var note;
  client.on("message", function (topic, message) {
    note = message.toString();
    // Updates React state with message
    setMsg(note);
    console.log(note);
    client.end();
  });

  // Sets default React state
  const [msg, setMsg] = useState(
    <Fragment>
      <em>...</em>
    </Fragment>
  );

  return (
    <div className="App">
      <header className="App-header">
        <h1>Hello MQTT in React</h1>
        <p>The message payload is: {msg}</p>
      </header>
    </div>
  );
}
export default App;

Step 4 Change the Settings for Webpack 5

Run the React project from your terminal.

npm start

When you start the app, if you are getting following error,

constants.js:46 Uncaught ReferenceError: Buffer is not defined
    at Object../node_modules/mqtt/node_modules/mqtt-packet/constants.js (constants.js:46:1)
    at Object.options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:61:1)
    at Object../node_modules/mqtt/node_modules/mqtt-packet/parser.js (parser.js:4:1)
    at Object.options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:61:1)
    at Object../node_modules/mqtt/node_modules/mqtt-packet/mqtt.js (mqtt.js:1:1)

The error states that the buffer is not defined. You can view this error in the console of your browser.

Follow the steps below to overcome this error.

  • inside of node_modules/mqtt create webpack.config.js:
const webpack = require('webpack')
module.exports = {
    entry: "./lib/connect/index.js",
    //mode: "development",
    output: {
        library:  {
            type: "commonjs2"
        },
        filename: "mqtt.browser.js"
    },
    plugins: [
        // fix "process is not defined" error:
        // (do "npm install process" before running the build)
        new webpack.ProvidePlugin({
          process: 'process/browser',
        }),
        new webpack.ProvidePlugin({
            Buffer: [ 'buffer', 'Buffer' ],
        }),
    ],
    resolve: {
        fallback: {
            assert: require.resolve('assert'),
            buffer: require.resolve('buffer'),
            console: require.resolve('console-browserify'),
            constants: require.resolve('constants-browserify'),
            crypto: require.resolve('crypto-browserify'),
            domain: require.resolve('domain-browser'),
            events: require.resolve('events'),
            http: require.resolve('stream-http'),
            https: require.resolve('https-browserify'),
            os: require.resolve('os-browserify/browser'),
            path: require.resolve('path-browserify'),
            punycode: require.resolve('punycode'),
            process: require.resolve('process/browser'),
            querystring: require.resolve('querystring-es3'),
            stream: require.resolve('stream-browserify'),
            string_decoder: require.resolve('string_decoder'),
            sys: require.resolve('util'),
            timers: require.resolve('timers-browserify'),
            tty: require.resolve('tty-browserify'),
            url: require.resolve('url'),
            util: require.resolve('util'),
            vm: require.resolve('vm-browserify'),
            zlib: require.resolve('browserify-zlib'),
        }
    }
}
  • inside of node_modules/mqtt/package.json
  1. add "type": "commonjs" (not sure if this is necessary)
  2. under "browser" change ./mqtt.js": "./lib/connect/index.js" to "./mqtt.js": "./dist/mqtt.browser.js"
{
  ...
  "type": "commonjs",
  "browser": {
    "./mqtt.js": "./dist/mqtt.browser.js"
    ...
  }
  ...
}
  • inside of node_modules/mqtt execute
npm i 
npm i buffer process
npm i webpack webpack-cli
npx webpack
  • clean npm cache
delete node_modules/.cache folder
  • Rebuild you app
npm start

Credits: https://github.com/mqttjs/MQTT.js/issues/1412

Step 5 Publish MQTT Messages from the MQTTBox

MQTTBox is a cross-platform application that makes it easy to create MQTT clients, virtual device networks, and load test MQTT devices and brokers. It lets you publish messages to an MQTT broker, subscribe to MQTT topics, receive messages, and do load testing. In this tutorial, we will use MQTTBox as an MQTT client and connect to our Mosquitto broker which is already running on a Raspberry Pi.

First, you have to create your MQTT client via the MQTTBox. Enter the host address ( in this case, it’s the IP address of my Pi), your username, and password. The client name can be any string of your own choice.

Next, enter the topic name where you would like to publish and the payload. Click Publish.

In your App.js from your react project, you have already set the username, password and the Host address. Also, you have subscribed from the same topic from where you have published using MQTTBox.

Step 6 Start your React Project

Run the following command from the terminal.

PS C:\Users\Monisha Vasumacharla\Desktop\mqtt-dashboard> npm start

Step 7 Final Output

Click on that link http://localhost:3001 which opens in the browser.

Happy Learning!

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

How to use MQTTBox to debug MQTT messages?

How to use MQTTBox to debug MQTT messages?

MQTTBox is a versatile MQTT (Message Queuing Telemetry Transport) client tool that facilitates testing and debugging of MQTT-based applications. MQTT is a lightweight messaging protocol commonly used in IoT (Internet of Things) and other scenarios where low-bandwidth,...

Create a Simple ReactJs Application – Part 1

Create a Simple ReactJs Application – Part 1

ReactJs is one of the famous front-end open-source libraries of JavaScript developed by Facebook. It aims to allow developers to quickly create fast user interfaces for websites and applications. In this blog, we will create a simple and basic ReactJs...

Create a Simple ReactJs Application – Part 2

Create a Simple ReactJs Application – Part 2

In the tutorial's last part, we discussed about ReactJs and how to run a simple react app on a web browser. In this part, we will dig more into some interesting stuff, such as creating a home, about us, and contact pages. Click here for the first part - Create a...

How to enable Mosquitto MQTT over WebSocket on Windows

How to enable Mosquitto MQTT over WebSocket on Windows

WebSocket is one of the communication protocols which provides full duplex communication over a single TCP/IP connection. It uses HTTP as a intial connection establishment. The WebSocket enables the communication from the web browser (client) to the server, in which...

MQTT Mosquitto Broker on Windows via Windows PowerShell

MQTT Mosquitto Broker on Windows via Windows PowerShell

Eclipse Mosquitto is an open-source message broker (EPL/EDL licensed) that supports MQTT versions 5.0, 3.1.1, and 3.1. The MQTT protocol uses a publish/subscribe method to deliver a lightweight messaging method. This makes it outstanding for Internet of Things (IoT)...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....