adamvr/MQTT.js — Issue #7: MQTT messages seem to be sent mutlple times
Author: observant
Created: 2012-02-06 11:48:24+00:00
State: open

I am following your example of the broadcast server and pub client but have come across an issue where the messages seem to be sent more than once

```javascript
var mqtt = require('mqttjs');

var server = mqtt.createServer(function(client) {
var clients = {};

client.on('connect', function(packet) {
console.log("[connect] %s", JSON.stringify(packet));
client.connack({returnCode: 0});
clients[client.id] = client;
});

client.on('publish', function(packet) {
console.log("[publish] %s", JSON.stringify(packet));

for (var clientId in clients) {
clients[clientId].publish({topic: packet.topic, payload: packet.payload});
}
});

client.on('subscribe', function(packet) {
console.log("[subscribe] %s", JSON.stringify(packet));
});

client.on('pingreq', function(packet) {
console.log("[pingreq] %s", JSON.stringify(packet));
});

client.on('disconnect', function(packet) {
console.log("[disconnect] %s", JSON.stringify(packet));
});

client.on('close', function(packet) {
console.log("[close] %s", JSON.stringify(packet));
});

client.on('error', function(packet) {
console.log("[error] %s", JSON.stringify(packet));
});
});
server.listen(9999);
console.log("Listening on port 9999");
```

Here is my pub client

```javascript
/**
* Created by JetBrains WebStorm.
* User: jima
* Date: 6/02/12
* Time: 9:58 PM
* To change this template use File | Settings | File Templates.
*/
var mqtt = require('mqttjs');
var argv = process.argv;

for (var index = 2; index <= 4; index++) {
if (!argv[index]) {
process.exit(-1);
}

var port = argv[2];
var topic = argv[3];
var payload = argv[4];

var client = mqtt.createClient(port, "localhost", function(client) {
client.connect({keepalive: 3});

client.on('connack', function(packet) {
if (packet.returnCode === 0) {
client.publish({topic: topic, payload: payload});
client.disconnect();
} else {
console.log("connack error %d", packet.returnCode);
process.exit(-1);
}
});

client.on('close', function() {
process.exit(0);
});

client.on('error', function(error) {
console.log("error %s", error);
process.exit(-1);
})
});
}
```

and here is the output

```
Listening on port 9999
[connect] {"cmd":"connect","retain":false,"qos":0,"dup":false,"length":24,"version":"MQIsdp","versionNum":3,"clean":false,"keepalive":3,"client":"mqtt_53593"}
[connect] {"cmd":"connect","retain":false,"qos":0,"dup":false,"length":24,"version":"MQIsdp","versionNum":3,"clean":false,"keepalive":3,"client":"mqtt_53593"}
[connect] {"cmd":"connect","retain":false,"qos":0,"dup":false,"length":24,"version":"MQIsdp","versionNum":3,"clean":false,"keepalive":3,"client":"mqtt_53593"}
[publish] {"cmd":"publish","retain":false,"qos":0,"dup":false,"length":11,"topic":"jima","messageId":28001,"payload":"hello"}
[disconnect] {"cmd":"disconnect","retain":false,"qos":0,"dup":false,"length":0}
[publish] {"cmd":"publish","retain":false,"qos":0,"dup":false,"length":11,"topic":"jima","messageId":28001,"payload":"hello"}
[disconnect] {"cmd":"disconnect","retain":false,"qos":0,"dup":false,"length":0}
[publish] {"cmd":"publish","retain":false,"qos":0,"dup":false,"length":11,"topic":"jima","messageId":28001,"payload":"hello"}
[disconnect] {"cmd":"disconnect","retain":false,"qos":0,"dup":false,"length":0}
```

Is this part of the QoS?