I am new to node.js am trying to make a webhook, where I get an HTTP post request and want to send a request over mqtt and wait for mqtt messages in reference to the MQTT message and then send these messages a response over HTTP
var array = [];
const client = mqtt.connect(MQTTServer)
var count =0;
client.on('message', (topic, message) => {
array[count] = message
count ++
}
app.post('/tesr', function (request, response) {
client.publish ('outTopic' , 'request ');
client.subscribe('inTopic')
//wait for multiple mqtt message in MQTT callback
//after all messages received or timeout return here
client.unsubscribe('inTopic')
count = 0
response.status(200).json(array);
}
so have tried while()
and seInterval()
but haven't found any sollution
You don't need to call
response.send(array)
from within the route handler, you can do it externally.It's not pretty (and it's single call at a time...) but it should work.