mqttjs / mqtt-connection

Barebone Connection object for MQTT in node.js
Other
114 stars 25 forks source link

subscribed client doesn't receive the message from broker #22

Open nick1008 opened 5 years ago

nick1008 commented 5 years ago

node version: 8.11.0 working on windows 7.

The client is successfully subscribed.

The broker receives the publish from client A but doesn't forward it to client B

`server.on('connection', function (stream) { let conn = mqttConn(stream)

// conn connected
conn.on('connect', function (packet) {
    console.log(conn.options);
    // acknowledge the connect packet
    conn.connack({ returnCode: 0 });
    })

conn.on('publish', function (packet) {
    times++;
    packet2send = packet;
    console.log(packet);
    conn.pubrec({ messageId: packet.messageId });
    conn.on('pubrel', function (packet) {
        console.log(packet);
        conn.pubcomp({ messageId: packet.messageId });
        stream.setTimeout(10000)
        stream.on('timeout', function () {
            conn.publish({
                retain: false,
                qos: 2,
                dup: false,
                length: 14,
                topic: 'hello',
                payload: 'world',
                messageId: 1
            });
            conn.on('pubrec', function (packet) {
                console.log('aa' + packet);
                conn.pubrel({ messageId: packet.messageId });
                console.log(packet);
                conn.on('pubcomp', function (packet) {
                    console.log('aa' + packet);
                    console.log(packet);
                });
            })
        });
    })
})

// conn subscribed
conn.on('subscribe', function (packet) {
    console.log(packet);
// send a suback with messageId and granted QoS level
    conn.suback({ granted: [packet.qos], messageId: messageId })
})

// timeout idle streams after 5 minutes
//stream.setTimeout(1000 * 60 * 5)

// connection error handling
conn.on('close', function (packet) { conn.destroy() 
    console.log('close ' + packet);})
conn.on('error', function (packet) { conn.destroy() 
    console.log('error ' + packet);})
conn.on('disconnect', function (packet) { conn.destroy() 
    console.log('disconnect' + packet);})

})`

nuharaf commented 5 years ago

mqtt-connection does not deal with topic matching and packet forwarding, for that you can use aedes . You can use mqtt-connection to built custom broker, but it is on itself is not a broker.

nick1008 commented 5 years ago

Yes. thank you very much for the reply!! I just figured that out 1 hour before you answer and used aedes. works fine. So balanced shame with a bit of confidence.. :P