Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

client.on get stuck waiting for message #796

Open
pejoujarrar opened this issue Jun 12, 2019 · 2 comments
Open

client.on get stuck waiting for message #796

pejoujarrar opened this issue Jun 12, 2019 · 2 comments

Comments

@pejoujarrar
Copy link

`exports.get_live_measure = function(req, res) {
var client = mqtt.connect('mqtt://192.168.1.193');
client.publish(req.params.typeId,req.params.sensorId)
client.subscribe('response')
client.on('message', function (topic, message) {
res.json(message.toString());
client.end();
});

};`

In this code, when i'm sending an API call for some devices who are not supposed to give a response, the code get stuck at the instruction client.on('message').

Is there any way to skip stop/skip the instruction after an amount of time? or a way to manage this situation.

@crapthings
Copy link

what if put subscribe and onMessage after connected?

const mqtt = require('mqtt')

const client  = mqtt.connect('mqtt://localhost')

client.on('connect', function () {
  client.subscribe('converted', function (err) {
    if (err) return
    client.on('message', function (topic, message) {
      const result = JSON.parse(message.toString())
      console.log(result)
    })
  })
})

@pejoujarrar
Copy link
Author

@crapthings thanks for the answer, but unfortunately nothing changes. the problem actually comes from the client.on("message") which enter in an infinite loop waiting for the response, but for an inexistant device there is no response, consequently, it will stick permanently there waiting for a never sent response, until i restart the server.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants