{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/mqtt-in-nodejs/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"70d3a11a-670c-5a46-8f08-68f675fa722c","excerpt":"In this article i will explain you how you can work with MQTT in NodeJS application Step 1: Install MQTT package Step 2: Add the following code to your Nodejs…","html":"<p>In this article i will explain you how you can work with MQTT in NodeJS application</p>\n<h4>Step 1: Install MQTT package</h4>\n<pre><code class=\"language-js\">npm install mqtt --save\n\n</code></pre>\n<h4>Step 2: Add the following code to your Nodejs application</h4>\n<pre><code class=\"language-js\">const mqtt = require(\"mqtt\");\nconst client = mqtt.connect(\"mqtt://test.mosquitto.org\");\n\nclient.on(\"connect\", function () {\n  client.subscribe(\"presence\", function (err) {\n    if (!err) {\n      client.publish(\"presence\", \"Hello mqtt\");\n    }\n  });\n});\n\nclient.on(\"message\", function (topic, message) {\n  // message is Buffer\n  console.log(message.toString());\n  client.end();\n});\n</code></pre>\n<p>-<strong><em>In this code we are connecting with IOT application and subscribing and publishing to the topic</em></strong></p>\n<p>In MQTT we generally communicate with IOT application using topic where application publish message to the topic and other application who have subscribed to this topic get the message</p>\n<pre><code class=\"language-js\">client.on(\"connect\", function () {\n  client.subscribe(\"presence\", function (err) {\n    if (!err) {\n      client.publish(\"presence\", \"Hello mqtt\");\n    }\n  });\n});\n</code></pre>\n<ul>\n<li><strong><em>Getting message from the topic</em></strong></li>\n</ul>\n<p>This is the code where we get message from the topic</p>\n<pre><code class=\"language-js\">client.on(\"message\", function (topic, message) {\n  // message is Buffer\n  console.log(message.toString());\n  client.end();\n});\n</code></pre>","fields":{"slug":"/mqtt-in-nodejs/"},"frontmatter":{"title":"MQTT in NodeJS application","date":"November 28, 2022","description":"In this article i will explain you how you can work with MQTT in NodeJS application","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mqtt-nodejs.jpeg"}},"previous":{"fields":{"slug":"/mongodb-creating-shardedcluster/"},"frontmatter":{"title":"MongoDB Sharding - Step By Step With Example","date":"November 15, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mongo.jpg"}},"next":{"fields":{"slug":"/node-express-typescript-mongo-rest-api/"},"frontmatter":{"title":"Developing REST Api using typescript, node, express and mongodb","date":"December 03, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/RestAPI.jpg"}}},"pageContext":{"id":"70d3a11a-670c-5a46-8f08-68f675fa722c","previousPostId":"613a2d5b-3600-502e-8676-56a4afe8de81","nextPostId":"39dc311c-1ef8-5938-a4aa-60266cea747e"}},
    "staticQueryHashes": ["3860684146"]}