import mqtt from "mqtt";
export default defineComponent({
props: {
mqtt: {
type: "app",
app: "mqtt",
}
},
async run({ steps, $ }) {
const url = `${this.mqtt.$auth.protocol}://${this.mqtt.$auth.host}:${this.mqtt.$auth.port}`;
const options = {
clientId: this.mqtt.$auth.client_id ?? `pipedream-client-${Date.now()}`,
connectTimeout: 5000,
username: this.mqtt.$auth.username,
password: this.mqtt.$auth.password,
};
const client = mqtt.connect(url, options);
await new Promise((resolve) => {
client.on("connect", () => {
client.publish("pipedream/test", 'Welcome MQTT + Pipedream users! ' + new Date().toISOString(), () => {
client.end();
console.log("Message sent");
resolve();
});
});
});
return {
mqttClientIsConnected: client.connected,
};
},
})
MQTT uses API keys for authentication. When you connect your MQTT account, Pipedream securely stores the keys so you can easily authenticate to MQTT APIs in both code and no-code steps.