Skip to content

Instantly share code, notes, and snippets.

@anhldbk
Last active December 25, 2023 20:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhldbk/71b6a377ada986aca63f5456a4192936 to your computer and use it in GitHub Desktop.
Save anhldbk/71b6a377ada986aca63f5456a4192936 to your computer and use it in GitHub Desktop.
Remove MQTT retained messages using MQTT.js version 1.10.x
var mqtt    = require('mqtt');
var lodash = require('lodash');
var client  = mqtt.connect({ host: 'localhost', port: 1883 });

client.on('connect', function () {
  console.log('Connected')
  client.subscribe('presence');
});


client.on('message', function (topic, message) {
  // message is Buffer
  console.log(message.toString());
  
  // remove the retained message
  client.publish('presence', undefined, {retain: true});
});
@AnthonyLzq
Copy link

Thanks man, this saved me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment