Condition based http response

On http in node we are sending a request to the MQTT broker using mqtt out node and on MQTT in node response we need to trigger the HTTP response.

we tried with below but doesnt seem to work,
can anyone please help me with this

[{"id":"fddbf80b980767ff","type":"mqtt in","z":"5e7c412c8b50ab24","name":"","topic":"in/topic1","qos":"2","datatype":"auto","broker":"9dcfb076.0d327","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":260,"wires":[["725f7f15da51da50"]]},{"id":"f10aa70b8d84e061","type":"mqtt out","z":"5e7c412c8b50ab24","name":"","topic":"out/topic1","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"9dcfb076.0d327","x":580,"y":100,"wires":[]},{"id":"aabd5ea787546e9d","type":"http in","z":"5e7c412c8b50ab24","name":"","url":"/getdata","method":"get","upload":false,"swaggerDoc":"","x":200,"y":100,"wires":[["f10aa70b8d84e061","725f7f15da51da50"]]},{"id":"2861f2a6e8074be2","type":"http response","z":"5e7c412c8b50ab24","name":"","statusCode":"","headers":{},"x":770,"y":180,"wires":[]},{"id":"725f7f15da51da50","type":"function","z":"5e7c412c8b50ab24","name":"","func":"if(msg.payload != null) \n{\n msg.statusCode=200;\n}\nelse\n{\n msg.statusCode=500;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":180,"wires":[["2861f2a6e8074be2"]]},{"id":"eb7f4d41c1652939","type":"comment","z":"5e7c412c8b50ab24","name":"","info":"On recieveing the HTTP request and its respective respone from MQTT in node , \nwe want to send the HTTP response code as 200 with some payload data or else 500\n\nand this process should repeat for all the http in requests","x":510,"y":240,"wires":[]},{"id":"9dcfb076.0d327","type":"mqtt-broker","name":"local","broker":"127.0.0.1","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"5","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

As described in the help text for the http request node, the message sent to the response node must contain msg.res as generated by the request node. So you must include that in the data passed to mqtt and then pick it up again when you get it back from mqtt.

This is a little tricky because to solve this you have to somehow wait for the mqtt-in node's reply and then as Colin said pass the msg.res (and the msg.req ?) to the http-response to complete the request/response chain.

Usually you use a Join node to achieve this .. wait for 2 x msgs, merge them and only then send to
http-response but i didnt manage to get it to work this way.

so i thought of using some Javascript to create a setInterval to monitor when a reply from the mqtt-in is saved in flow Context and then send the msg to http-response.

Here is a test flow:

[{"id":"68efb1a6e926fe14","type":"mqtt in","z":"54efb553244c241f","name":"","topic":"in/topic1","qos":"2","datatype":"json","broker":"9dcfb076.0d327","nl":false,"rap":true,"rh":0,"inputs":0,"x":240,"y":760,"wires":[["5c3197b364cc0942"]]},{"id":"721ad3b2d592b8d5","type":"mqtt out","z":"54efb553244c241f","name":"","topic":"out/topic1","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"9dcfb076.0d327","x":580,"y":520,"wires":[]},{"id":"3a076ee9cba4d241","type":"http in","z":"54efb553244c241f","name":"","url":"/getdata","method":"get","upload":false,"swaggerDoc":"","x":290,"y":520,"wires":[["721ad3b2d592b8d5","8c7be3a037d4ed3a","679f8de88a4d59aa"]]},{"id":"73817ab0b5bfea95","type":"http response","z":"54efb553244c241f","name":"","statusCode":"","headers":{"content-type":"application/json"},"x":890,"y":620,"wires":[]},{"id":"8c7be3a037d4ed3a","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":460,"wires":[]},{"id":"52f6e1cc6a67633f","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":760,"wires":[]},{"id":"d1dcd218c664595a","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":680,"wires":[]},{"id":"679f8de88a4d59aa","type":"function","z":"54efb553244c241f","name":"monitor reply","func":"node.status({ fill: \"green\", shape: \"dot\", text: \"Waiting mqtt..\" });  // set Node status\n\n// timer to check for mqtt reply\nlet timer = setInterval(() => {\n\n    let mqttReply = flow.get(\"mqttReply\")\n    if (mqttReply) {\n        msg.payload = mqttReply  // attach mqttReply to payload\n        flow.set(\"mqttReply\", undefined)  // clear flow Context\n        node.send(msg);  // send msg\n        node.status({});  // clear status\n        clearInterval(timer)  // self clear timer\n\n    }\n}, 250)\n\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":620,"wires":[["73817ab0b5bfea95","d1dcd218c664595a"]]},{"id":"5c3197b364cc0942","type":"function","z":"54efb553244c241f","name":"set Flow Context","func":"flow.set(\"mqttReply\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":760,"wires":[["52f6e1cc6a67633f"]]},{"id":"9dcfb076.0d327","type":"mqtt-broker","name":"local","broker":"127.0.0.1","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"5","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Should be possible to use a join
here is an example

[{"id":"3a076ee9cba4d241","type":"http in","z":"c791cbc0.84f648","name":"","url":"/getdata","method":"get","upload":false,"swaggerDoc":"","x":120,"y":1320,"wires":[["8c7be3a037d4ed3a","fc500b45.3094e","722d2d8c.d1cccc"]]},{"id":"8c7be3a037d4ed3a","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":340,"y":1260,"wires":[]},{"id":"fc500b45.3094e","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"in","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":1360,"wires":[["1bb2d2b.5fa732d"]]},{"id":"722d2d8c.d1cccc","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$random()*100","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":1320,"wires":[["721ad3b2d592b8d5"]]},{"id":"1bb2d2b.5fa732d","type":"join","z":"c791cbc0.84f648","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":510,"y":1380,"wires":[["73817ab0b5bfea95","d1dcd218c664595a"]]},{"id":"721ad3b2d592b8d5","type":"mqtt out","z":"c791cbc0.84f648","name":"","topic":"out/topic1","qos":"","retain":"","broker":"e8ba3ef5.22f4a8","x":540,"y":1320,"wires":[]},{"id":"e2553edb.6846f","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"mqtt","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":1420,"wires":[["1bb2d2b.5fa732d"]]},{"id":"73817ab0b5bfea95","type":"http response","z":"c791cbc0.84f648","name":"","statusCode":"","headers":{"content-type":"application/json"},"x":690,"y":1360,"wires":[]},{"id":"d1dcd218c664595a","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":1420,"wires":[]},{"id":"68efb1a6e926fe14","type":"mqtt in","z":"c791cbc0.84f648","name":"","topic":"out/topic1","qos":"2","datatype":"json","broker":"e8ba3ef5.22f4a8","x":90,"y":1420,"wires":[["e2553edb.6846f"]]},{"id":"dbdfdb57.97bfd8","type":"inject","z":"c791cbc0.84f648","name":"test mqtt in","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"test\":1}","payloadType":"json","x":140,"y":1560,"wires":[["d2c987bb.d82608"]]},{"id":"d2c987bb.d82608","type":"http request","z":"c791cbc0.84f648","name":"","method":"GET","ret":"txt","paytoqs":"query","url":"http://192.168.1.25:1880/getdata","tls":"","persist":false,"proxy":"","authType":"","x":340,"y":1560,"wires":[["62dacdbd.ae3fd4"]]},{"id":"62dacdbd.ae3fd4","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":1560,"wires":[]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"node-red","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Unborn I think your mqtt is not using same topic.

1 Like

I know .. only because Jinesh had a different topic in his flow .. in my tests i changed it like yours.

Nice solution by the way .. you got it working .. much cleaner than using timers

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.