Created
January 8, 2021 05:13
-
-
Save Craigson/161cd042de458ddb34c1a790d5adfeff to your computer and use it in GitHub Desktop.
Publisher NodeJS app for the Syntropy Stack MQTT NodeJS example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mqtt = require("mqtt"); | |
| const moment = require("moment"); | |
| const client = mqtt.connect("mqtt://172.20.0.2:1883"); | |
| const CronJob = require("cron").CronJob; | |
| // publish a message at 5minute intervals | |
| const job = new CronJob( | |
| "5 * * * * *", | |
| function () { | |
| const time = moment().format("MMMM Do YYYY, h:mm:ss a"); | |
| console.log(`[sending] ${time}`); | |
| client.publish("hello_syntropy", `Powered by SyntropyStack: ${time}`); | |
| }, | |
| null, | |
| true, | |
| "America/New_York" | |
| ); | |
| console.log("Initializing Publisher"); | |
| client.on("connect", function () { | |
| console.log("Established connection with Broker"); | |
| client.publish("init", `Powered by SyntropyStack`); | |
| job.start(); | |
| }); | |
| client.on("message", function (topic, message) { | |
| // message is Buffer, so convert to string | |
| console.log(message.toString()); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment