Skip to content

Instantly share code, notes, and snippets.

@AnthonyLzq
Created January 24, 2023 03:03
Show Gist options
  • Save AnthonyLzq/23e8c8e7c9d15ad720ba651fce325975 to your computer and use it in GitHub Desktop.
Save AnthonyLzq/23e8c8e7c9d15ad720ba651fce325975 to your computer and use it in GitHub Desktop.
Script to create a MQTT client and connect to it using MQTT.js
import mqtt from 'mqtt'
const options: mqtt.IClientOptions = {
port: process.env.MQTT_PORT ? parseInt(process.env.MQTT_PORT) : 0,
host: process.env.MQTT_HOST,
protocol: 'mqtts',
keepalive: 0,
username: process.env.MQTT_USER,
password: process.env.MQTT_PASS
}
const client = mqtt.connect(options)
client.on('connect', () => {
console.log('Connected to HiveMQ')
})
export { client }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment