Multiple TCP servers and correct response

Hi, i want to use Node Red to configure for example two TCP servers.
One will be listening on port 5000, second on port 6000, both running in one flow.

I have two aplications on PC - one tries to connect to TCP port 5000, second to TCP port 6000, but when connecting, it does not send any data. It just opens the TCP connection.

What I need to achieve in Node red is to send the different responses to clients who connected to port 5000 and different to the clients which connected to port 6000.
If i use two TCP in nodes, set as "Listen on port 5000" and "listen on port 6000" and two TCP out nodes, set as "Reply to TCP", then when I send something to TCP out node, it sends the packet also to clients on port 5000 and on 6000. I know about the _session variable, but I can not read it, because the application which is connecting to node red is not sending any data when connecting, so I am not able to read the _session property. So how to solve it? Thanks.

Hi @jttjtt

The trick here is to use the Status node.
It will emit the properties required by the TCP Reply method, when a client connects.

It could be made a little more compact maybe, but should do what you need.

[{"id":"02c9110b356fb24d","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"7ccc8e36a3bed2a0","type":"tcp in","z":"02c9110b356fb24d","name":"","server":"server","host":"","port":"5000","datamode":"stream","datatype":"buffer","newline":"","topic":"","trim":false,"base64":false,"tls":"","x":225,"y":255,"wires":[["16d0daee5a5eada1"]]},{"id":"81c4cfed94a3860d","type":"status","z":"02c9110b356fb24d","name":"get session for 6000","scope":["302fde5716edc7fa"],"x":250,"y":155,"wires":[["36ca293c537788f9"]]},{"id":"eb79e8684bc8a543","type":"change","z":"02c9110b356fb24d","name":"","rules":[{"t":"set","p":"C6000","pt":"flow","to":"status._session","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":150,"wires":[[]]},{"id":"302fde5716edc7fa","type":"tcp in","z":"02c9110b356fb24d","name":"","server":"server","host":"","port":"6000","datamode":"stream","datatype":"buffer","newline":"","topic":"","trim":false,"base64":false,"tls":"","x":225,"y":310,"wires":[["71bc710d74c4620b"]]},{"id":"3b1ef32aac335848","type":"status","z":"02c9110b356fb24d","name":"get session for 5000","scope":["7ccc8e36a3bed2a0"],"x":250,"y":110,"wires":[["54f99caffd8c7350"]]},{"id":"319e5fb8d64d648d","type":"change","z":"02c9110b356fb24d","name":"","rules":[{"t":"set","p":"C5000","pt":"flow","to":"status._session","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":70,"wires":[[]]},{"id":"241abfd517f6e0e5","type":"tcp out","z":"02c9110b356fb24d","name":"","host":"","port":"","beserver":"reply","base64":false,"end":false,"tls":"","x":560,"y":255,"wires":[]},{"id":"ba656090023f1cef","type":"tcp out","z":"02c9110b356fb24d","name":"","host":"","port":"","beserver":"reply","base64":false,"end":false,"tls":"","x":565,"y":310,"wires":[]},{"id":"54f99caffd8c7350","type":"switch","z":"02c9110b356fb24d","name":"","property":"status.event","propertyType":"msg","rules":[{"t":"eq","v":"connect","vt":"str"},{"t":"eq","v":"disconnect","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":110,"wires":[["319e5fb8d64d648d"],["94d5ed987e3cd803"]]},{"id":"94d5ed987e3cd803","type":"change","z":"02c9110b356fb24d","name":"","rules":[{"t":"delete","p":"C5000","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":110,"wires":[[]]},{"id":"36ca293c537788f9","type":"switch","z":"02c9110b356fb24d","name":"","property":"status.event","propertyType":"msg","rules":[{"t":"eq","v":"connect","vt":"str"},{"t":"eq","v":"disconnect","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":155,"wires":[["eb79e8684bc8a543"],["3bbd0fcc32e64e0d"]]},{"id":"3bbd0fcc32e64e0d","type":"change","z":"02c9110b356fb24d","name":"","rules":[{"t":"delete","p":"C6000","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":185,"wires":[[]]},{"id":"16d0daee5a5eada1","type":"function","z":"02c9110b356fb24d","name":"Set Target","func":"const Target = flow.get(\"C6000\")\n\nif(Target){\n    msg._session = flow.get(\"C6000\")\n    return msg\n}\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":255,"wires":[["241abfd517f6e0e5"]]},{"id":"71bc710d74c4620b","type":"function","z":"02c9110b356fb24d","name":"Set Target","func":"const Target = flow.get(\"C5000\")\n\nif(Target){\n    msg._session = flow.get(\"C5000\")\n    return msg\n}\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":395,"y":310,"wires":[["ba656090023f1cef"]]},{"id":"725c01aca20b4828","type":"debug","z":"02c9110b356fb24d","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":435,"wires":[]},{"id":"4de9831d6d7b4578","type":"inject","z":"02c9110b356fb24d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":265,"y":520,"wires":[["574ef95a779aeb6b"]]},{"id":"7737678484ea5f04","type":"tcp in","z":"02c9110b356fb24d","name":"","server":"client","host":"127.0.0.1","port":"6000","datamode":"stream","datatype":"buffer","newline":"","topic":"","trim":false,"base64":false,"tls":"","x":275,"y":435,"wires":[["725c01aca20b4828"]]},{"id":"574ef95a779aeb6b","type":"tcp request","z":"02c9110b356fb24d","name":"","server":"127.0.0.1","port":"5000","out":"sit","ret":"buffer","splitc":" ","newline":"","trim":false,"tls":"","x":630,"y":515,"wires":[[]]}]
1 Like

Hi @marcus-j-davies
thanks a lot for the sample flow. I have made a small change, I added nodes to genrate sample data to be sent to port 6000 and 5000, here it is:

[
    {
        "id": "02c9110b356fb24d",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "7ccc8e36a3bed2a0",
        "type": "tcp in",
        "z": "02c9110b356fb24d",
        "name": "",
        "server": "server",
        "host": "",
        "port": "5000",
        "datamode": "stream",
        "datatype": "utf8",
        "newline": "\\n",
        "topic": "",
        "trim": false,
        "base64": false,
        "tls": "",
        "x": 220,
        "y": 440,
        "wires": [
            [
                "16d0daee5a5eada1",
                "abb1a9f5fca611ca"
            ]
        ]
    },
    {
        "id": "81c4cfed94a3860d",
        "type": "status",
        "z": "02c9110b356fb24d",
        "name": "get session for 6000",
        "scope": [
            "302fde5716edc7fa"
        ],
        "x": 250,
        "y": 155,
        "wires": [
            [
                "36ca293c537788f9"
            ]
        ]
    },
    {
        "id": "eb79e8684bc8a543",
        "type": "change",
        "z": "02c9110b356fb24d",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "C6000",
                "pt": "flow",
                "to": "status._session",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 150,
        "wires": [
            []
        ]
    },
    {
        "id": "302fde5716edc7fa",
        "type": "tcp in",
        "z": "02c9110b356fb24d",
        "name": "",
        "server": "server",
        "host": "",
        "port": "6000",
        "datamode": "stream",
        "datatype": "utf8",
        "newline": "\\n",
        "topic": "",
        "trim": false,
        "base64": false,
        "tls": "",
        "x": 220,
        "y": 495,
        "wires": [
            [
                "71bc710d74c4620b",
                "407c540ca47bd0a8"
            ]
        ]
    },
    {
        "id": "3b1ef32aac335848",
        "type": "status",
        "z": "02c9110b356fb24d",
        "name": "get session for 5000",
        "scope": [
            "7ccc8e36a3bed2a0"
        ],
        "x": 250,
        "y": 110,
        "wires": [
            [
                "54f99caffd8c7350"
            ]
        ]
    },
    {
        "id": "319e5fb8d64d648d",
        "type": "change",
        "z": "02c9110b356fb24d",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "C5000",
                "pt": "flow",
                "to": "status._session",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 70,
        "wires": [
            []
        ]
    },
    {
        "id": "241abfd517f6e0e5",
        "type": "tcp out",
        "z": "02c9110b356fb24d",
        "name": "",
        "host": "",
        "port": "",
        "beserver": "reply",
        "base64": false,
        "end": false,
        "tls": "",
        "x": 865,
        "y": 445,
        "wires": []
    },
    {
        "id": "ba656090023f1cef",
        "type": "tcp out",
        "z": "02c9110b356fb24d",
        "name": "",
        "host": "",
        "port": "",
        "beserver": "reply",
        "base64": false,
        "end": false,
        "tls": "",
        "x": 865,
        "y": 505,
        "wires": []
    },
    {
        "id": "54f99caffd8c7350",
        "type": "switch",
        "z": "02c9110b356fb24d",
        "name": "",
        "property": "status.event",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "connect",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "disconnect",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 450,
        "y": 110,
        "wires": [
            [
                "319e5fb8d64d648d"
            ],
            [
                "94d5ed987e3cd803"
            ]
        ]
    },
    {
        "id": "94d5ed987e3cd803",
        "type": "change",
        "z": "02c9110b356fb24d",
        "name": "",
        "rules": [
            {
                "t": "delete",
                "p": "C5000",
                "pt": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 710,
        "y": 110,
        "wires": [
            []
        ]
    },
    {
        "id": "36ca293c537788f9",
        "type": "switch",
        "z": "02c9110b356fb24d",
        "name": "",
        "property": "status.event",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "connect",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "disconnect",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 450,
        "y": 155,
        "wires": [
            [
                "eb79e8684bc8a543"
            ],
            [
                "3bbd0fcc32e64e0d"
            ]
        ]
    },
    {
        "id": "3bbd0fcc32e64e0d",
        "type": "change",
        "z": "02c9110b356fb24d",
        "name": "",
        "rules": [
            {
                "t": "delete",
                "p": "C6000",
                "pt": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 710,
        "y": 185,
        "wires": [
            []
        ]
    },
    {
        "id": "16d0daee5a5eada1",
        "type": "function",
        "z": "02c9110b356fb24d",
        "name": "Set Target",
        "func": "const Target = flow.get(\"C5000\")\n\nif(Target){\n    msg._session = flow.get(\"C5000\")\n    return msg\n}\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 385,
        "y": 440,
        "wires": [
            [
                "241abfd517f6e0e5"
            ]
        ]
    },
    {
        "id": "71bc710d74c4620b",
        "type": "function",
        "z": "02c9110b356fb24d",
        "name": "Set Target",
        "func": "const Target = flow.get(\"C6000\")\n\nif(Target){\n    msg._session = flow.get(\"C6000\")\n    return msg\n}\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 390,
        "y": 495,
        "wires": [
            [
                "ba656090023f1cef"
            ]
        ]
    },
    {
        "id": "725c01aca20b4828",
        "type": "debug",
        "z": "02c9110b356fb24d",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 660,
        "wires": []
    },
    {
        "id": "4de9831d6d7b4578",
        "type": "inject",
        "z": "02c9110b356fb24d",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 275,
        "y": 745,
        "wires": [
            [
                "574ef95a779aeb6b"
            ]
        ]
    },
    {
        "id": "7737678484ea5f04",
        "type": "tcp in",
        "z": "02c9110b356fb24d",
        "d": true,
        "name": "",
        "server": "client",
        "host": "127.0.0.1",
        "port": "6000",
        "datamode": "stream",
        "datatype": "buffer",
        "newline": "",
        "topic": "",
        "trim": false,
        "base64": false,
        "tls": "",
        "x": 285,
        "y": 660,
        "wires": [
            [
                "725c01aca20b4828"
            ]
        ]
    },
    {
        "id": "574ef95a779aeb6b",
        "type": "tcp request",
        "z": "02c9110b356fb24d",
        "d": true,
        "name": "",
        "server": "127.0.0.1",
        "port": "5000",
        "out": "sit",
        "ret": "buffer",
        "splitc": " ",
        "newline": "",
        "trim": false,
        "tls": "",
        "x": 640,
        "y": 740,
        "wires": [
            []
        ]
    },
    {
        "id": "407c540ca47bd0a8",
        "type": "debug",
        "z": "02c9110b356fb24d",
        "name": "TCP 6000 prijem",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 425,
        "y": 545,
        "wires": []
    },
    {
        "id": "abb1a9f5fca611ca",
        "type": "debug",
        "z": "02c9110b356fb24d",
        "name": "TCP 5000 prijem",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 410,
        "y": 380,
        "wires": []
    },
    {
        "id": "f50df83545e79565",
        "type": "inject",
        "z": "02c9110b356fb24d",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 490,
        "y": 320,
        "wires": [
            [
                "4d27d1893da65991"
            ]
        ]
    },
    {
        "id": "4d27d1893da65991",
        "type": "function",
        "z": "02c9110b356fb24d",
        "name": "Sample data for port 5000",
        "func": "const Target = flow.get(\"C5000\")\n\nif (Target) {\n    msg._session = flow.get(\"C5000\")\n \n}\nlet x = Math.floor((Math.random() * 200000) + 10000);\nmsg.payload = \"Port 5000: \" + x + \"\\r\\n\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 320,
        "wires": [
            [
                "241abfd517f6e0e5"
            ]
        ]
    },
    {
        "id": "26b8a07258857617",
        "type": "inject",
        "z": "02c9110b356fb24d",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 650,
        "y": 580,
        "wires": [
            [
                "4ebbfb334546900f"
            ]
        ]
    },
    {
        "id": "4ebbfb334546900f",
        "type": "function",
        "z": "02c9110b356fb24d",
        "name": "Sample data for port 6000",
        "func": "const Target = flow.get(\"C6000\")\n\nif (Target) {\n    msg._session = flow.get(\"C6000\")\n \n}\nlet x = Math.floor((Math.random() * 200000) + 10000);\nmsg.payload = \"Port 6000: \" + x + \"\\r\\n\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 880,
        "y": 580,
        "wires": [
            [
                "ba656090023f1cef"
            ]
        ]
    }
]

There are only some problems, i have :slight_smile:

  1. The flow works only with one connected client to port 5000 and one to port 6000. So if two clients connect to port 5000, only one recieves the data... This is a problem, but lets say not so big now.
    2.Bigger problem is, than when Clients conect to port 5000 and then port 6000, the data is sent OK to both of them. But when one clients disconnects, than all data are sent to the second client. I was testing it using Hercules utilitz and you can see, that I have closed connestion 5000 on the left side of windows and then on the right side also data desired to port 5000 come to port 6000.

To allow multi-client:

When the status Node emits a connect, add the session to an array object, instead of a single object using a function node (you will need to manage the removals also)

let Clients = flow.get("Clients") || []
Clients.push(msg.status._session)
flow.set("Clients",Clients)

Then when a client sends a message, filter out its self from that array

const Clients = flow.get("Clients")
const Targets = Clients.filter((C) => C.id !== msg._session.id)


/* Use async send */
for(let i = 0;i<Targets.length;i++){
    node.send({
        _session: Targets[i],
        payload: msg.payload
    })
}

The above should address your issues, unless I am missing something.
Of course we don't really know the end goal here.

Here is an extremely optimised approach, and at the same time - will allow multi client only using 1 port.

A lot of this is just guess work

Not tested

[{"id":"2eecb40eb7fc4fb1","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"b1fd4367bc90908c","type":"tcp in","z":"2eecb40eb7fc4fb1","name":"","server":"server","host":"","port":"5000","datamode":"stream","datatype":"buffer","newline":"","topic":"","trim":false,"base64":false,"tls":"","x":325,"y":170,"wires":[["3eb8825649a5efcd"]]},{"id":"b6b0850665e63e0d","type":"status","z":"2eecb40eb7fc4fb1","name":"","scope":["b1fd4367bc90908c"],"x":325,"y":100,"wires":[["efded0bec1794014"]]},{"id":"efded0bec1794014","type":"function","z":"2eecb40eb7fc4fb1","name":"Add/Remove sessions","func":"let Clients = flow.get(\"Clients\") || []\n\nif(msg.status.event === 'connect') {\n    \n    Clients.push(msg.status._session)\n    flow.set(\"Clients\",Clients)\n    \n} else if (msg.status.event === 'disconnect') {\n    \n    if(Clients.length){\n       Clients = Clients.filter((C) => C.id !== msg.status._session.id)\n       flow.set(\"Clients\",Clients)\n    }\n}\n\n","outputs":0,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":535,"y":100,"wires":[]},{"id":"3eb8825649a5efcd","type":"function","z":"2eecb40eb7fc4fb1","name":"Send to all but self","func":"const Clients = flow.get(\"Clients\")\nconst Targets = Clients.filter((C) => C.id !== msg._session.id)\n\n/* Use async send */\nfor(let i = 0;i<Targets.length;i++){\n    node.send({\n        _session: Targets[i],\n        payload: msg.payload\n    })\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":165,"wires":[["bb79b97e022f45a6"]]},{"id":"bb79b97e022f45a6","type":"tcp out","z":"2eecb40eb7fc4fb1","name":"","host":"","port":"","beserver":"reply","base64":false,"end":false,"tls":"","x":755,"y":165,"wires":[]}]

HI @marcus-j-davies ,

i will describe what I want to achieve:

  1. On one PC there is NODE RED app listening on ports TCP 5000 and 6000
  2. On the same PC i have two applications simulating connection to port TCP 5000 and 6000 to Node red.

What I want to achieve is, that I want to be able to send messages to the application which has connected to port 5000 and different messages to application which has connected to port 6000.

I think that the SessionId will help, but is not working good in this sample flows.
At the end, if it will be great to be possible to have MultiClients - so more clients from different PC connecting to port 5000 and 6000 and be able to communicate with them, so the messages will be sent only to those clients which are connecting either to port 5000 or 6000. If it is not clear enough, I will try to explain better.

So as well as storing the session, store the port they have connected to also.
And filter out not just their session, but also the port.

If the same app instance needs to receive the same message on the other port - just remove that filter part.

It's all there :wink:

[{"id":"2eecb40eb7fc4fb1","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"b6b0850665e63e0d","type":"status","z":"2eecb40eb7fc4fb1","name":"5000","scope":["b1fd4367bc90908c"],"x":200,"y":130,"wires":[["41bc9afb2530c48e"]]},{"id":"efded0bec1794014","type":"function","z":"2eecb40eb7fc4fb1","name":"Add/Remove sessions","func":"let Clients = flow.get(\"Clients\") || []\n\nif(msg.status.event === 'connect') {\n    Clients.push(msg.status._session)\n    flow.set(\"Clients\",Clients)\n    \n} else if (msg.status.event === 'disconnect') {\n    \n    if(Clients.length){\n       Clients = Clients.filter((C) => C.id !== msg.status._session.id)\n       flow.set(\"Clients\",Clients)\n    }\n}\n\n","outputs":0,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":835,"y":155,"wires":[]},{"id":"bb79b97e022f45a6","type":"tcp out","z":"2eecb40eb7fc4fb1","name":"","host":"","port":"","beserver":"reply","base64":false,"end":false,"tls":"","x":1020,"y":415,"wires":[]},{"id":"41bc9afb2530c48e","type":"change","z":"2eecb40eb7fc4fb1","name":"","rules":[{"t":"set","p":"status._session.connectedToPort","pt":"msg","to":"5000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":465,"y":130,"wires":[["efded0bec1794014"]]},{"id":"e0495cc38164ad22","type":"status","z":"2eecb40eb7fc4fb1","name":"6000","scope":["f225a64319668241"],"x":200,"y":175,"wires":[["b92c22f5943ef8fb"]]},{"id":"b92c22f5943ef8fb","type":"change","z":"2eecb40eb7fc4fb1","name":"","rules":[{"t":"set","p":"status._session.connectedToPort","pt":"msg","to":"6000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":465,"y":175,"wires":[["efded0bec1794014"]]},{"id":"b1fd4367bc90908c","type":"tcp in","z":"2eecb40eb7fc4fb1","name":"","server":"server","host":"","port":"5000","datamode":"stream","datatype":"buffer","newline":"","topic":"","trim":false,"base64":false,"tls":"","x":210,"y":325,"wires":[["661ad02eb19f03ba"]]},{"id":"f225a64319668241","type":"tcp in","z":"2eecb40eb7fc4fb1","name":"","server":"server","host":"","port":"6000","datamode":"stream","datatype":"buffer","newline":"","topic":"","trim":false,"base64":false,"tls":"","x":215,"y":380,"wires":[["5fd17df0abaf97f4"]]},{"id":"3eb8825649a5efcd","type":"function","z":"2eecb40eb7fc4fb1","name":"Send to all","func":"const Clients = flow.get(\"Clients\")\nconst Targets = Clients.filter((C) => C.connectedToPort === msg.topic)\n\n/* Use async send */\nfor(let i = 0;i<Targets.length;i++){\n    node.send({\n        _session: Targets[i],\n        payload: msg.payload\n    })\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":865,"y":470,"wires":[["bb79b97e022f45a6"]]},{"id":"644c13c379b83a61","type":"inject","z":"2eecb40eb7fc4fb1","name":"A message for all Clients connected to port 5000","props":[{"p":"payload"},{"p":"topic","v":"5000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A message for all Clients connected to port 5000","payloadType":"str","x":365,"y":455,"wires":[["3eb8825649a5efcd"]]},{"id":"50daae15cf7c823f","type":"inject","z":"2eecb40eb7fc4fb1","name":"A message for all Clients connected to port 6000","props":[{"p":"payload"},{"p":"topic","v":"6000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A message for all Clients connected to port 6000","payloadType":"str","x":360,"y":510,"wires":[["3eb8825649a5efcd"]]},{"id":"388a40e614833b19","type":"function","z":"2eecb40eb7fc4fb1","name":"Send to other ports (excluding self)","func":"const Clients = flow.get(\"Clients\")\nconst Targets = Clients.filter((C) => C.connectedToPort === msg.forPortUsersOf && C.id !== msg._session.id)\n\n/* Use async send */\nfor(let i = 0;i<Targets.length;i++){\n    node.send({\n        _session: Targets[i],\n        payload: msg.payload\n    })\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":790,"y":350,"wires":[["bb79b97e022f45a6"]]},{"id":"661ad02eb19f03ba","type":"change","z":"2eecb40eb7fc4fb1","name":"","rules":[{"t":"set","p":"forPortUsersOf","pt":"msg","to":"6000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":325,"wires":[["388a40e614833b19"]]},{"id":"5fd17df0abaf97f4","type":"change","z":"2eecb40eb7fc4fb1","name":"","rules":[{"t":"set","p":"forPortUsersOf","pt":"msg","to":"5000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":380,"wires":[["388a40e614833b19"]]}]

1 Like

Hi @marcus-j-davies ,

I can write only - It works like a charm! Great work and really very nice and clean flow.
Thanks a lot for help - where can I send you beer, or something? :slight_smile:

3 Likes

Just to enjoy the community is good enough! :+1:

3 Likes

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