Multiple Connections on TCP in/out reply Node

511 views
Skip to first unread message

Chua Jiahao

unread,
May 3, 2017, 11:44:35 PM5/3/17
to Node-RED
Hi,
I have a TCP in node set to receive data from various sensor controllers, and sends this data to a backend via mqtt, while optionally send a response based on several conditions. This system has to handle multiple simultaneous connections on the same port. When the response is triggered by the input, this works fine and the response is sent to the correct connection on the port.

However, there are also cases where the response is initiated based on an mqtt message from the backend.
A simplified diagram of the situation is like below:

The backend sends messages in response to the data, thus the connection will still be open. 
However, how do I ensure that this message gets sent to the correct connection if there are multiple connections? Is there a way to track the connections and ensure it gets sent to a specific connection? Or is there a better way of achieving the same setup that I am not aware of?

Thanks for any advice.

Julian Knight

unread,
May 4, 2017, 1:58:58 AM5/4/17
to Node-RED
As you are already using MQTT, a possibly better way is to disaggregate the two parts of the flow. 

From your TCP input use a function to identify the source and use that to populate the msg.topic and pass the msg out to MQTT.

In a separate flow, listen to the required topics and process as needed.

This also makes things a lot easier to change in the future since you could easily add a web API or other sensors that send direct to MQTT or anything you like - all going into the same or similar flows that output to MQTT, the processing flow would need minimal changes.

Chua Jiahao

unread,
May 4, 2017, 2:34:56 AM5/4/17
to Node-RED
Hi Julian,
Thanks for the reply.

If I were to use MQTT topics to split the flow, how would I then ensure that I send the response to the right connection? I assume I am restricted to one set of tcp in and tcp reply node if I want to have multiple devices share the same port.

Dave C-J

unread,
May 4, 2017, 3:49:55 AM5/4/17
to node...@googlegroups.com
The tcp out node can be used in reply mode. This uses the
​_​
session  property
​(is an object with both type and id), ​
of the msg to send it to the correct session. Data from your
​m​
qtt branch will need to have that added in the function node if you only want that data to go to one session, so you would need to store the
​_​
session you need in the
​function ​
node context.

Chua Jiahao

unread,
May 4, 2017, 5:34:44 AM5/4/17
to Node-RED
Hi Dave,
Thanks so much for that information. Its exactly what I needed. Very much appreciated.
Reply all
Reply to author
Forward
0 new messages