In Node-Red, I would like to be able to send a message and wait for its response using MQTT, in between an HTTP input node and a response one. I think a picture is more relevant to explain this:
The problem is an HTTP response node has to be directly linked to an input one somehow, so I thought of saving the whole message in context after receiving an HTTP input, then restore it before sending the response. The problem is that apparently, the structure of the message is circular, hence I can't directly convert it to a string, to store it in context.
I've tried different things to solve this issue (I had a look at this: JSON.stringify, avoid TypeError: Converting circular structure to JSON and I tried using the circular-json module, neither of which work).
This is somewhat breaking the asynchronous nature of MQTT, while Pub/Sub can be used it's not it's strong point.
You have to consider what happens if there is no response message, since there is no way to know if the other MQTT client is connected.
Where is the HTTP request coming from? If it's from a web page then you could use MQTT over Websockets to make the request directly or plain Websockets , removing the need for the HTTP call all together.
Having said that you should be able to store the msg
object in the context, there is no need to convert it to a string as the default context storage engine is just a in memory object store. So just add object to the context and retrieve it when the response comes in.
You may be able to do something with the trigger node to deal with the time out issue.