MQTT using Node.js with practical examples

DonskyTech
11 Jan 202310:49

Summary

TLDRThis video tutorial demonstrates how to communicate with an MQTT broker using Node.js for both CLI and web applications. It showcases a simple CLI project and an Express web application, both interacting with a local Mosquitto MQTT broker. The tutorial uses the 'mqtt' package for Node.js, explaining how to install it and implement MQTT communication with sample code for subscriber and publisher functionalities. The video also includes a web application demo, showing real-time message exchange between publisher and subscriber through a browser interface.

Takeaways

  • 📚 The video demonstrates communication with an MQTT broker using Node.js runtime.
  • 🔌 Two practical applications are built: a CLI project and an Express web application.
  • 📝 The local Mosquitto MQTT broker is used for communication with Node.js.
  • 🛠️ The 'mqtt' npm package is utilized for MQTT communication in both applications.
  • 🔧 Installation of the 'mqtt' package is done using 'npm install mqtt'.
  • 📑 The CLI project includes 'subscriber.js' and 'publisher.js' scripts.
  • 📡 The subscriber waits for messages on subscribed topics, while the publisher sends messages to topics.
  • 🔄 The MQTT broker facilitates message exchange between the Node.js applications and itself.
  • 🌐 The Express web application uses the same 'mqtt' package for browser-based interaction.
  • 🖥️ The web application runs on port 3000, with separate pages for publishing and subscribing to MQTT topics.
  • 🔍 The video includes a detailed explanation and code examples available on GitHub.

Q & A

  • What is the main topic of the video?

    -The video is about demonstrating how to communicate with an MQTT broker in a Node.js runtime environment using two example applications: a command-line interface (CLI) project and an Express web application.

  • What are the two example applications built in the video?

    -The two example applications are a CLI project that can be run in a shell or terminal, and an Express web application that can be accessed through a web browser.

  • What MQTT broker is used in the video?

    -The video uses a local Mosquitto MQTT broker for communication with the Node.js runtime.

  • What package is used to facilitate the communication with the MQTT broker in the Node.js applications?

    -The 'mqtt' package is used, which can be installed using the command 'npm install mqtt'.

  • Where can the complete code for the projects shown in the video be found?

    -The complete code for the projects is available in the presenter's GitHub repository, linked in the description of the video.

  • What are the two main JavaScript files used in the CLI project?

    -The two main JavaScript files for the CLI project are 'subscriber.js' and 'publisher.js'.

  • How does the subscriber.js file work in the CLI project?

    -The subscriber.js file subscribes to a particular topic and waits for any messages from the MQTT broker on that topic.

  • How does the publisher.js file work in the CLI project?

    -The publisher.js file publishes messages to a specified topic on the MQTT broker.

  • What is the purpose of the 'mqtt.connect' function in the video?

    -The 'mqtt.connect' function is used to establish a connection to the MQTT broker.

  • What happens when a message is published using the publisher.js file?

    -When a message is published using publisher.js, it is sent to the specified topic and received by the subscriber connected to that topic, as well as reflected in the Mosquitto broker terminal.

  • How does the video demonstrate the communication between the publisher and subscriber?

    -The video demonstrates communication by showing the process of publishing a message to a topic and receiving it on the subscriber side, with both applications (CLI and web) interacting with the Mosquitto MQTT broker.

  • What web framework is used in the Express web application project?

    -The Express web application project uses the Express web framework with EJS as the templating engine.

  • How can users interact with the MQTT broker through the web application?

    -Users can subscribe and publish messages to topics through the web application's interface, which communicates with the MQTT broker.

  • What is the URL for accessing the publisher and subscriber pages in the web application?

    -The URL for the publisher and subscriber pages is not explicitly mentioned in the script, but the web application runs on port 3000, suggesting that the pages can be accessed through 'http://localhost:3000/publisher' and 'http://localhost:3000/subscriber' or similar endpoints.

  • How does the web application handle browser requests?

    -The web application handles browser requests through routes defined in the Express framework, which then interact with controllers and the service layer to process the requests.

  • What is the role of the 'mqtt.service.js' file in the web application?

    -The 'mqtt.service.js' file contains the code that connects to the MQTT broker and is used by the controller layer to handle MQTT communication.

  • How does the video conclude?

    -The video concludes by summarizing the demonstration of MQTT communication in a Node.js environment and directing viewers to the companion write-up and code in the video description for further details.

Outlines

00:00

😀 Communicating with MQTT Broker in Node.js CLI and Web Applications

This paragraph introduces a tutorial on how to communicate with an MQTT broker using Node.js. Two practical applications are discussed: a command-line interface (CLI) project and an Express web application. The tutorial utilizes a local Mosquitto MQTT broker and the 'mqtt' package for Node.js, which is installed using npm. The presenter demonstrates the process of subscribing and publishing messages between the broker and Node.js applications. The code for both the subscriber and publisher is explained, including the setup of connection details and callback functions for message events. The paragraph concludes with the presenter showing how to publish messages to a topic and receive them in the subscriber terminal, indicating successful communication.

05:02

🌐 Building a Browser-Based MQTT Application with Node.js and Express

The second paragraph delves into creating a web application that uses MQTT for communication. It leverages the 'mqtt' package and follows the standard Node.js and Express folder structure, including routes, controllers, and a service layer. The presenter demonstrates running the web application, which allows users to subscribe and publish messages through web pages. The subscriber page enables users to subscribe to topics and receive published messages, while the publisher page allows sending messages to the broker. The presenter also covers the code structure, including the service layer responsible for connecting to the MQTT broker, the controller layer that manages the webpage templates, and the routing that handles browser requests. The explanation includes the use of EJS as a templating engine and the overall setup of the Express server.

10:06

📝 Conclusion and Further Resources for MQTT in Node.js

The final paragraph wraps up the tutorial by summarizing the use of MQTT in a Node.js environment and encouraging further exploration. It mentions that the detailed write-up and code for the project can be found in the video description, providing a resource for those interested in learning more. The paragraph concludes with a prompt for viewers to check out the code and a sign-off, indicating the end of the tutorial.

Mindmap

Keywords

💡MQTT

MQTT, which stands for 'Message Queuing Telemetry Transport,' is a lightweight messaging protocol designed for limited bandwidth and unreliable networks. It is used for machine-to-machine communication and IoT applications. In the video, MQTT is central to the theme as the creator demonstrates how to communicate with an MQTT broker using Node.js, showcasing its use in both CLI and web applications.

💡Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript on the server side. It is the runtime environment used in the video to demonstrate how to interact with an MQTT broker. The script mentions building CLI and web applications using Node.js, highlighting its versatility in creating various types of applications.

💡CLI

CLI stands for Command Line Interface, which is a text-based interface used to interact with computer programs. In the video, the creator builds a CLI project that communicates with an MQTT broker, demonstrating the use of Node.js to create command-line applications that can subscribe to and publish messages on MQTT topics.

💡Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. The video showcases an Express web application that communicates with an MQTT broker, illustrating how Express can be used to create web applications that interact with IoT devices and services.

💡Mosquitto

Mosquitto is an open-source MQTT broker that enables the exchange of messages between devices. In the video, Mosquitto serves as the MQTT broker that the Node.js applications interact with, facilitating the demonstration of MQTT communication in both CLI and web applications.

💡Subscriber

In the context of MQTT, a subscriber is a client that receives messages on a specific topic. The video script describes a subscriber script that subscribes to an MQTT topic and waits for messages, exemplifying how a Node.js application can act as an MQTT subscriber.

💡Publisher

A publisher in MQTT is a client that sends messages to a specific topic. The video demonstrates a publisher script that publishes messages to an MQTT topic, showing how a Node.js application can act as an MQTT publisher.

💡npm

npm stands for Node Package Manager, a package manager for JavaScript and the default package manager for the JavaScript runtime environment Node.js. The video script includes a command 'npm install mqtt' to install the 'mqtt' package, which is used to connect Node.js applications to an MQTT broker.

💡EJS

EJS is a templating engine that allows developers to use plain JavaScript code to generate dynamic HTML. In the video, EJS is used in the Express web application to create HTML pages that interact with an MQTT broker, demonstrating its role in server-side rendering of web content.

💡Service Layer

In software architecture, the service layer is a logical layer that provides specific business functionality. In the video, the service layer contains the code connecting to the MQTT broker, abstracting the business logic from the presentation layer, which is a common practice in creating maintainable and scalable applications.

💡Controller Layer

The controller layer in software architecture is responsible for handling user input, performing operations on the model, and selecting the appropriate view to display. In the video, the controller layer interacts with the service layer to handle requests from the web application, demonstrating how it manages the flow of data between the user interface and the service layer.

Highlights

Introduction to communicating with an MQTT broker in a Node.js runtime.

Building two example applications: a CLI project and an Express web application.

Using a local Mosquitto MQTT broker for communication.

Installation of the 'mqtt' package using npm.

CLI project setup with subscriber.js and publisher.js.

Demonstration of subscribing to a topic and receiving messages.

Publishing messages to the same topic from a separate terminal.

Verification of message exchange between subscriber and publisher.

Simple code structure for MQTT communication in Node.js.

Creating a browser-based web application using the same MQTT package.

Node.js and Express folder structure for the web application.

Running the web application and accessing publisher and subscriber pages.

Subscribing and publishing messages through the web interface.

Unsubscribing from a topic to stop receiving messages.

Re-subscribing to receive messages again after an unsubscribe.

Code explanation for the service layer connecting to the MQTT broker.

Controller layer handling browser requests and returning responses.

Use of app.js for setting up the server and routes.

Companion write-up and code availability in the video description.

Transcripts

play00:00

hi welcome to Don's gator in this video

play00:03

I'm going to show you how you can

play00:06

communicate with your mqtt broker in a

play00:09

node.js runtime

play00:11

we are building two example practical

play00:14

application here one is a command line

play00:17

interface or a CLI project that you can

play00:20

run in your shell or your terminal and

play00:23

the other is an Express web application

play00:26

that you can run using your browser in

play00:29

both this project I am using my local

play00:32

mosquito mqtt broker which communicates

play00:37

with my node.js runtime and bought my

play00:40

CLI application and my Express web

play00:43

application communicates with my mqtt

play00:47

broker to the mqtt message exchange

play00:51

between mosquito and node.js in what

play00:55

this project we are gonna be using an NM

play00:59

package called the mqtt

play01:02

and all you have to do is to install it

play01:05

using this command which is npm install

play01:09

fqtt I have already done this so I will

play01:13

not be executing this command now let's

play01:16

go over to my first project which is my

play01:19

CLI project

play01:21

the complete code that I am showing you

play01:24

here is available in my GitHub

play01:26

repository which you can find in the

play01:29

description of this video

play01:31

I have here

play01:33

my subscriber WPS and my publisher.js

play01:38

and in the background is my mosquito

play01:41

mqtt broker

play01:45

so I have here to split terminal

play01:49

the left side is for the publisher and

play01:52

the right side is for that subscriber

play01:54

let's just arrange the screen

play02:01

if I execute the command node

play02:04

subscriber.js

play02:06

then you would notice that that this was

play02:09

reflected in my mosquito broker terminal

play02:12

that I have subscribed to a particular

play02:15

topics since I have subscribed

play02:19

then this terminal will wait for any

play02:22

messages coming from my mosquito mqtt

play02:25

broker in a separate terminal we are

play02:28

going to publish some messages on the

play02:31

same topic to do this I'm going to

play02:34

execute the command node

play02:36

publisher.js so let's try publishing

play02:39

some message on the same puppy

play02:43

as you can see I am subscribed to the

play02:46

topic temperature so in my node

play02:48

publisher.js I'm going to publish

play02:52

something to the same topic which is

play02:54

temperature

play02:56

as you can see I am sending the topic

play02:59

temperature and the message 32 and if

play03:03

you check out the message from the

play03:06

subscriber terminal you would see that I

play03:09

have received the message 32 on the

play03:11

topic temperature same also with my

play03:14

mosquito mqtt broker you have noticed

play03:17

that the publish was successfully

play03:20

received by both the subscriber and the

play03:24

publisher if we take a look at the code

play03:26

it is really simple as I just import my

play03:29

mqtt package here and set my and set my

play03:34

connection details here I have a

play03:37

function called connect broker that

play03:39

connects to my qtt mosquito broker and

play03:45

using this command which is

play03:47

mqtt.connect and I have function here

play03:52

called callback functions that gets

play03:55

called

play03:56

whenever something happened like this

play03:59

message event which basically just logs

play04:03

the topic and messages received from the

play04:06

broker

play04:07

to subscribe from any topic we just need

play04:10

to execute this command and that's it it

play04:14

will wait for any published messages on

play04:17

the same topic if we go over to the

play04:20

publisher.js then it has the same mqtt

play04:24

connect functions and the only

play04:27

difference here is this published

play04:29

message functions which basically

play04:31

publish a message on a particular topic

play04:35

that is basically how easy it is to

play04:39

communicate with an mqtt broker using

play04:43

this code

play04:44

and if you just follow these plants here

play04:48

then you can create your own CLI

play04:50

applications that communicates with your

play04:53

own mqtt broker tli applications might

play04:57

be a bit boring so if you wanted to

play05:01

create your own browser-based web

play05:04

application

play05:06

then you can still leverage the same

play05:08

mqtt and PM package I have here a node

play05:13

Express mqtt web application that uses

play05:17

the express web framework I will be

play05:20

following the usual node.js and express

play05:23

folder structure where I have routes

play05:26

controllers

play05:28

applied processing and using ejs here in

play05:32

a service layer that communicates with

play05:34

our acute mosquito broker

play05:38

again the complete hood is in my GitHub

play05:41

account and for demo purpose I'm going

play05:44

to run this project let's go back into

play05:47

my

play05:48

third Visual Studio code and in this

play05:51

command I'm just going to execute npm

play05:54

run and then

play06:03

you can see it says that and now able to

play06:07

listen to at the application running in

play06:10

Port 3000.

play06:16

so

play06:17

when you run the project we have here

play06:20

the URL for my publisher page

play06:26

and

play06:28

I have here the URL for my subscriber

play06:34

on the subscriber page we can subscribe

play06:38

to any topic like I'm pretty sure

play06:40

temperature temperature

play06:46

and we click the Subscribe button you

play06:49

would notice that it says that the

play06:51

status bar here that you are subscribed

play06:53

if I publish something in on the

play06:56

publisher page

play06:59

like

play07:00

I click 32 and click publish

play07:04

then you would notice that

play07:06

it was displayed in the message section

play07:08

of the publisher page let's try again so

play07:12

28

play07:14

and then 31

play07:17

now if I unsubscribe to the same Peak

play07:21

temperature then I am expecting that I

play07:23

should not be receiving any mqtt message

play07:26

from my publisher so I click unsubscribe

play07:29

now I am unsubscribed then I'll send in

play07:32

another message

play07:35

notice nothing is happening let's try

play07:37

again 40.

play07:40

okay nothing is happening if I click the

play07:42

Subscribe button again I'm subscribed

play07:45

then if I type in a new temperature and

play07:49

I publish then you would notice that it

play07:51

gets published that's right for the last

play07:53

time

play07:54

then you would notice that the publisher

play07:59

is able to send messages into the

play08:01

subscriber through my mosquito mqtt

play08:04

broker but the publisher and the

play08:07

subscriber application is running in my

play08:10

node.js project

play08:16

take a look at the code that I have here

play08:19

the usual node.js directory structure

play08:23

where I have here the controllers

play08:26

the routes the service layer and the

play08:30

templating engine

play08:32

using the ejs

play08:34

if we take a look at the service layer

play08:37

The mqtt

play08:38

service.js Click contains our it

play08:41

contains our code that will connect to

play08:43

our mqtt broker

play08:46

which is almost similar to what we have

play08:49

discussed in the CLI application the

play08:52

service layer is used by the controller

play08:54

layer here

play08:56

the controller layer in turns returns

play08:59

our ejs template

play09:01

like this one which is our HTML pages

play09:08

like here

play09:10

and for the subscriber

play09:16

so

play09:17

the routes on the other hand like this

play09:20

one handles our browser request if it is

play09:24

get or a post request then it asks the

play09:29

controller

play09:30

to handle the request

play09:32

the return of the controller could

play09:34

either be an HTML or a Json data

play09:39

all of these components are tied up by

play09:42

our app.js file server

play09:45

which sets up the static files to serve

play09:49

our static assets and set that view

play09:54

engine to ejs and we have here the

play09:57

parsing of the request and several

play10:00

routes that we have created also

play10:06

and you can see here that it is using

play10:09

the Subscribe router and the publish

play10:11

router and it runs and listen to the

play10:14

port 3000

play10:16

that is all for the code actually depend

play10:20

on how you could leverage mqtt in a

play10:23

node.ap.js environment the companion

play10:26

write up of this video contains much

play10:28

detailed explanation of this project the

play10:32

right app and the code can be found in

play10:34

the description of this video I hope you

play10:36

learned something public exploring

play10:39

foreign

play10:43

[Music]

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
MQTT BrokerNode.jsCLI ProjectWeb ApplicationMosquittoMessage Exchangenpm PackageSubscriberPublisherExpress.js
هل تحتاج إلى تلخيص باللغة الإنجليزية؟