In this article, I will be walking you through how to use MQTT node.js library.
You might have heard about the buzz word “IOT” or Internet of things. Yes, MQTT is the most commonly used protocol in internet of things. There are two key things one has to understand i.e. the publisher and the subscriber. Let us see some background about publisher and subscriber. Subscriber is the one which is interesting in receiving messages which are published by publisher. You can have one or more subscribers registered to receive messages. Each of these client gets subscribed to messages via topic or topics. Publisher is the one, which is responsible for publishing the messages. All clients which are subscribed to a specific topic or topics will get notified with the message published by the publisher. We are building a sample “nodejs” client and server application using MQTT library to demonstrate how we can take advantage MQTT nodejs library is subscribing and publishing messages with ease. Node.js is very powerful, simple and easy to use. One can really take advantage of building simple and elegant applications using “nodejs”. First, before we start working on the sample application, there are couple of things we have to do. Yes, that’s installing “MQTT” node.js library and some of the dependencies. Setting up the dependencies and required node.js libraries Step 1: Download and install Python 2.7.10 Step 2: Execute the following command in command prompt. npm config set python C:\Python\python.exe Step 3: Install MQTT node.js library by executing the following command. npm install –g mqtt Demo App Here’s the scenario for which we are coding the node.js application. For demonstration purpose, we are making use of the following JSON weather forecast sample. Sample forecast. MQTT Node.js Client The following is the code snippet for MQTT client, where you can see the client is getting subscribed to a specific topic – “mydevice/forecast”. The handleMessage is something which gets called asynchronously when the client receive message from the publisher.
Pro WPF: Windows Presentation Foundation in .NET 3.0