moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

Server received Published event many times #678

Closed phuonglv6 closed 6 years ago

phuonglv6 commented 6 years ago

I got a issue when I setup demo project: Server received published event when client send one published message for example code: Server.js: var mosca = require('mosca')

var settings = { port: 1883 }; //here we start mosca var server = new mosca.Server(settings); server.on('ready', setup);

// fired when the mqtt server is ready function setup() { console.log('Mosca server is up and running') }

// fired whena client is connected server.on('clientConnected', function(client) { console.log('client connected', client.id); });

// fired when a message is received server.on('published', function(packet, client) { console.log('Published : ', packet.payload); });

// fired when a client subscribes to a topic server.on('subscribed', function(topic, client) { console.log('subscribed : ', topic); });

// fired when a client subscribes to a topic server.on('unsubscribed', function(topic, client) { console.log('unsubscribed : ', topic); });

// fired when a client is disconnecting server.on('clientDisconnecting', function(client) { console.log('clientDisconnecting : ', client.id); });

// fired when a client is disconnected server.on('clientDisconnected', function(client) { console.log('clientDisconnected : ', client.id); });

Client.js: var mqtt = require('mqtt')

var client = mqtt.connect({ host: 'localhost', port: 1883 }); client.subscribe('presence');

console.log('Client publishing.. '); client.publish('presence', 'Client 1 is alive.. Test Ping! ' + Date()); client.end();

Output Console in server:

[nodemon] starting node Server.js Mosca server is up and running client connected mqttjs_e87a3683 Published : mqttjs_e87a3683 subscribed : presence Published : <Buffer 43 6c 69 65 6e 74 20 31 20 69 73 20 61 6c 69 76 65 2e 2e 20 54 65 73 74 20 50 69 6e 67 21 20 54 75 65 20 53 65 70 20 31 39 20 32 30 31 37 20 31 33 3a ... > Published : {"clientId":"mqttjs_e87a3683","topic":"presence"} unsubscribed : presence clientDisconnected : mqttjs_e87a3683 Published : {"clientId":"mqttjs_e87a3683","topic":"presence"} Published : mqttjs_e87a3683

mcollina commented 6 years ago

you get a published event even for $SYS topics. These are metadata that Mosca publishes to support its own features. Check the topic and ignore them.