Last active
June 16, 2020 00:39
-
-
Save stmpjmpr/3de2945cb3835e6d96ef834b6c8cfbbd to your computer and use it in GitHub Desktop.
Calling PubNub via MQTT Bridge with JavaScript
This file contains 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
import mqtt from "mqtt"; | |
const publishKey = '<your_publish_key>'; | |
const subscribeKey = '<your_subscribe_key>'; | |
const clientId = 'pn-mqtt'; | |
const topic = 'pubnub_mqtt_test'; | |
const options = { | |
clientId: `${publishKey}/${subscribeKey}/${clientId}` | |
}; | |
const client = mqtt.connect('mqtt://mqtt.pndsn.com', options); | |
client.on('connect', () => { | |
client.subscribe(topic, err => { | |
if (!err) { | |
client.publish(topic, 'Hello PubNub from MQTT!') | |
} | |
}); | |
}); | |
client.on('message', (topic, message) => { | |
console.log(message.toString()) | |
client.end() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment