moscajs / mosca

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

message event emitted shows packet with wrong qos #435

Closed rclai closed 8 years ago

rclai commented 8 years ago
import mqtt from 'mqtt';

var client = mqtt.connect('ws://test:3234');

client.on('message', function(topic, payload) {
  console.log('message', arguments);
});

client.subscribe('test');
client.publish('test', 'hello', { qos:1 }, function() { console.log(arguments) });

The object emitted has the packet with qos 0 instead of qos 1. For some reason sometimes this goes from 0 to 1 and sometimes the event is not emitted at all.

arguments
0: "test"
1: Uint8Array[5]
2: Packet
   cmd: "publish"
   dup: false
   length: 24
   payload: Uint8Array[5]
   qos: 0
   retain: false
   topic: "test"

I'm testing with mosca server.

rclai commented 8 years ago

Ah I just found one part of the issue. When the MQTT server is down and client reconnection is happening, once the server is back up, and the reconnection is successful, that's when message event no longer emits anything anymore.

psorowka commented 8 years ago

@rclai I did not fully understand your first post.

your second post however is a clear case: you made the very common mistake of calling subscribe serially after connect. Instead, you should subscribe in the on('connect') callback; then it will resubscribe after every reconnect.

mcollina commented 8 years ago

@rclai as @psorowka stated.

Can you please provide steps to reproduce, all the time? Can you also verify if this happens only in the browser or also in Node.js?

mcollina commented 8 years ago

Closing for lack of activity, feel free to reopen.