'NODE-RED (Error: Cannot set headers after they are sent to the client) multiple devices sending request
I made a flow to a chatbot on node-red using the IBM cloud AI through http connections and I have the following problem, when sending simultaneous messages in different chats/accounts, this error occurs: "Error: Cannot set headers after they are sent to the client". I know it's related to the request that it can't be overridden. But what would be the way to have multiple devices accessing the same flow without interfering with the request already in progress?
I'm simulating sending messages through facebook messenger and instagram
thanks for the time
Solution 1:[1]
The last change node implies you are stashing the msg.res field somewhere and putting it back.
I'm guessing you are using the same context variable to do this so it's getting overwritten by the second invocation, so both events try to respond on the last msg.res and the second one fails because they can only be used once.
You shouldn't need to do this if all the nodes in the flow behave properly and don't discard msg properties. If there is a node discarding properties then you should raise an issue with it's author to get it fixed.
In the meantime you need to make sure each invocation keeps track of it's own msg.res which may mean storing it under a unique id (possibly msg._id but if a node is discarding properties this might also change) to they all respond to the correct session.
Solution 2:[2]
As @hardillb says the cause of the error is the bifurcation of your flow at the switch node. That means that you are sending two http responses back to the invoking client, when only one is allowed.
The Watson Assistant node puts its response in msg.payload, it doesn't reset, update, or remove any other fields.
I suspect that you don't need the bifurcation, in which case you can pass the flow from your set msg.res to the template and function node.
The only thing that your bifurcation might need to be preserve is msg.payload, in which case you can either save it in the flow context, or in some other msg field eg. msg.mypayload.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hardillb |
| Solution 2 | chughts |


