question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QOS 2

Hello, I’m going through a somewhat embarrassing situation, I disconnect the Client from the server, send new messages to the topic, but after the client connects, he does not receive the messages. is this normal in QOS 2? I read a very interesting topic: mqtt-essentials-part-6-mqtt-quality-of-service-levels - I’m testing QOS 2

SERVER:


require('dotenv').config();
const mongoose = require('mongoose');
const queryString = require('query-string');
const ClientIo = require('./client-io');
const boardSendRouter = require('./routes/board_send');
const boardRouter = require('./routes/board');
const appSendRouter = require('./routes/app_send');
const appRouter = new appSendRouter();
const url = require('url');
const aedes = require('aedes')();
const mqtt = require('mqtt');
const server = require('net').createServer(aedes.handle);
const port = 1883;


mongoose.connect(process.env.DATABASE_URL, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useFindAndModify: false,
});
server.listen(port, function() {
    console.log('Aedes listening on port:', port)
})

aedes.subscribe('board/submit_action', function(packet, cb) {
    try {
        var element = JSON.parse(packet.payload.toString());
        var topic = "board_" + element['id_board'];
        aedes.publish({
            topic: topic,
            payload: packet.payload.toString()
        })
        cb();
    } catch (e) {
        // declarações para manipular quaisquer exceções
        console.log(e); // passa o objeto de exceção para o manipulador de erro
    }
});



CLIENT ARDUINO:

    byte willQoS = 2;
    const char* willTopic = "arduino/status";
    const char* willMessage = "OFF_LINE";
    boolean willRetain = true;
    const char* username = "Placa_1";
    const char* password = "legal";
    boolean cleanSession = false; 

    if (clientMqtt.connect(id_board, username, password, willTopic, willQoS, willRetain, willMessage, cleanSession) { 
      clientMqtt.subscribe("board_5ffcf3e5c18f457afa091c19");
    }

API:


router.get('/', async (req, res, next) => {
    var result = {};
    var result = {
        success: false,
        message: "Invalido"
    };
    // var object = req.body;
    var object = req.query
    if (mongoose.Types.ObjectId.isValid(object.id_board)) {
        if (object.cod != '' && object.cod != '?' && object.cod != undefined) {
            const app_send = new AppSend({
                id_board: object.id_board,
                message: object.cod,
            });
            var save = await app_send.save();
            topic = "board/submit_action";
            var client = mqtt.connect('MYSERVER/mqtt', {
                username: 'Placa_1',
                password: 'legal',
                clientId: 'client_api',
            });
            client.on('connect', () => {
                client.publish(topic, JSON.stringify(save));
                // client.publish("arduino/status", "testekkk");
                client.end();
            });
            client.on('disconnect', () => {
                console.log('disconnect eita');
            });
            client.on('offline', () => {
                console.log('offline eita');
            });
            // client.end();
            // console.log("chegou na acao");
            result['success'] = true;
            result['message'] = "Ação enviada!";
            res.json(result);
            return;
        }
        var promise = await BoardSend.findOne({
            id_board: object.id_board
        }).sort({
            _id: -1
        }).limit(1);
        result = promise ? JSON.parse(promise.message) : {};
        res.json(result);
        return;
    } else {
        res.send(404);
        return;
        // var json = require('./teste-offline.json'); //with path
    }
});



Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
luan-nvgcommented, Jan 23, 2021

@luan-nvg Could you firstly try an example fully in nodejs or by using another mqtt client? If that is working it means it’s something wrong on the arduino code

I will do the test and give feedback #

0reactions
robertsLandocommented, Jan 27, 2021

What you are looking for is the retain flag (on published messages) or the clean flag (on connect). I suggest you to avoid using the clean flag as in most cases retain flag is enough. So set the option retain: true to the messages you send and when the client connects it will receive the last message sent to each topic


Daniel

On 27 Jan 2021, at 03:53, luan-nvg notifications@github.com wrote:

@robertsLando

when multiple requests are sent via Client nodeJs

has a message that is not coming client.publish(topic, JSON.stringify(params), { qos: 2 });

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSCun56276 - %ETH-QOS-2 ... - Cisco Bug
Bug information is viewable for customers and partners who have a service contract. Registered users can view up to 200 bugs per month...
Read more >
Mosquitto QoS2 bug - or subtle spec interpretation?
As I see no way to argue that my missing RX3 is not "QoS 1 and QoS 2 messages pending transmission to the...
Read more >
Bug: MQTT QOS 2 duplicated messages (IDFGH-7558) · Issue ...
I have experienced that when sending more than one message in quick succession that I sometimes receive duplicate messages in my server. Does ......
Read more >
[mosquitto-dev] MQTT QoS 2 protocol problem - Eclipse
I just want to verify whether this indeed is a protocol error of the client or not. This MQTT client is sending messages...
Read more >
MQTT - AWS IoT Core
The MQTT protocol defines a third level of QoS, level 2 , but AWS IoT does ... You can debug or process error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found