Problem with TCP requests

Hi,

I have some troubles with TCP requests. I'd like to send over TCP a flowContext variable with some string appended to it, and it seems I could be able to do that using JSONata in the inject node.

Problem is, when I send a simple string from a inject node, I get without any problem the answer from the server.
When using that JSONata formatting, I can see the message looks good from a debug node connected to the inject output, but the tcp server don't send anything back.

A bit more strange, when I send again the working string message from the other inject node, then I get a response from the TCP server including both the message from the string inject node AND the content fron the node set with JSONata content.....

Does someone have any idea about this ?

Also, is working with JSONata the good way to go ? I could use a simple inject and a function node, but I like to keep the NR layout as simple as possible....

Thanks,

Jerome

You have provided no information that we can help you with, we would be guessing as to the strings you are sending.
Possibly an example flow might be of help.

You could look at using the template node to add strings to a flow context variable. Read the sidebar help text for the node to see how.

Hi !

Thank you for your answer, and very sorry for the lack of detailed information, I realize I sent the message a bit too quickly.

Actually, I made my tests using one of the example flows from the TCP node .

By playing again with it, I think that maybe my problem is the special caracters used in some of the nodes in the example . I attach some pics as I'm not sure how they'll appear in another text editor :

From the "set result" change node :

image

Result in Debug :

image

I dont really understand, I can see the TCP request node use a \n to close the connection, but I don't think it's relevant here.... However the TCP in use the same kind of delimitation caracters as what is described in the working inject nodes....

Strangest thing to me is when I use the inject node with string ending with \n , I don't get any direct reply but it seems they are put in some sort of cache, and when I then use a working inject node I get all the messages at once, including the previous ones....

Maybe I don't really get how the TCP nodes work ?

Flow here :

[{"id":"d2402e3.cf533d","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"2e80c8be5fec29f9","type":"tcp in","z":"d2402e3.cf533d","name":"","server":"server","host":"localhost","port":"1883","datamode":"stream","datatype":"utf8","newline":"¥n","topic":"","trim":false,"base64":false,"tls":"","x":1230,"y":680,"wires":[["3ad5731de3d9aa2b"]]},{"id":"fe131238699f12ff","type":"inject","z":"d2402e3.cf533d","name":"Inject Expresion with ¥n","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"Hello, World!¥n\"","payloadType":"jsonata","x":1200,"y":460,"wires":[["5b281b1b21a6985e"]]},{"id":"f65e203a078b420d","type":"debug","z":"d2402e3.cf533d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":580,"wires":[]},{"id":"5b281b1b21a6985e","type":"tcp request","z":"d2402e3.cf533d","name":"","server":"localhost","port":"1883","out":"char","ret":"buffer","splitc":"\\n","newline":"","trim":false,"tls":"","x":1350,"y":580,"wires":[["b3d0580296ad9241"]]},{"id":"7bf9a55e1aa32de3","type":"tcp out","z":"d2402e3.cf533d","name":"","host":"","port":"","beserver":"reply","base64":false,"end":false,"tls":"","x":1530,"y":680,"wires":[]},{"id":"3ad5731de3d9aa2b","type":"change","z":"d2402e3.cf533d","name":"set result","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"Received: \" & payload & \"\b\\n\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":680,"wires":[["7bf9a55e1aa32de3"]]},{"id":"b3d0580296ad9241","type":"function","z":"d2402e3.cf533d","name":"Buffer to String","func":"msg.payload = msg.payload.toString();\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1540,"y":580,"wires":[["f65e203a078b420d"]]},{"id":"57561301fb1da8e3","type":"comment","z":"d2402e3.cf533d","name":"Send reply to client of TCP connection","info":"Input message from `TCP in` node may be passed to `TCP out` node to return a reply to client.\n","x":1230,"y":520,"wires":[]},{"id":"bd81a085e1bd3564","type":"comment","z":"d2402e3.cf533d","name":"↓ Accept request","info":"","x":1220,"y":640,"wires":[]},{"id":"eea92c66614eab02","type":"comment","z":"d2402e3.cf533d","name":"↓ Reply result","info":"","x":1550,"y":640,"wires":[]},{"id":"52bad1f87bc01676","type":"inject","z":"d2402e3.cf533d","name":"Inject string with ¥n","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"Hello, World!¥n\"","payloadType":"str","x":1190,"y":420,"wires":[["5b281b1b21a6985e"]]},{"id":"41a974b56932d651","type":"inject","z":"d2402e3.cf533d","name":"Inject String with \\n","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"Hello, World!\\n\"","payloadType":"str","x":1190,"y":380,"wires":[["5b281b1b21a6985e"]]},{"id":"1933ee27d9e25675","type":"inject","z":"d2402e3.cf533d","name":"Inject FlowContext+String","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$flowContext(\"MyFlowContext\") & \" blabla!¥n\"\t","payloadType":"jsonata","x":1210,"y":340,"wires":[["5b281b1b21a6985e"]]},{"id":"8e9c811c0dc922fd","type":"inject","z":"d2402e3.cf533d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1120,"y":180,"wires":[["fb5580bbbcde486a"]]},{"id":"fb5580bbbcde486a","type":"function","z":"d2402e3.cf533d","name":"Create Flow Context : MyFlowContext","func":"\nflow.set(\"MyFlowContext\", \"MyFlowContext\")\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1460,"y":180,"wires":[[]]},{"id":"ef3920de6ec79bfe","type":"comment","z":"d2402e3.cf533d","name":"Create flow variable","info":"","x":1130,"y":140,"wires":[]}]

Many thanks in advance !

Jerome

Maybe try \r as the delimiter instead of \n

The delimiter tells the tcp node that this is the end of the message, so it hold all received messages and once it receives the delimiter it then passes on all the messages received, that includes all messages without a delimiter and the final delimiter message.

thank you guys, will give it a try tomorrow and come back.

Hi there, I had a quick look today at work, but did not have time to work on it correctly.

I've seen that the TCP request node was not set to close after specific character received, so might be the origin of the problem.
What's still strange is that all inject nodes are first connected to a function node to add "\r\n" as it's a requirement of the device API, two of the inject nodes work as expected, but not the third one....

Anyway, I'll try to take time to investigate and hopefully solve it tomorrow.

Jerome

Hi all !

I keep working on it, but still have some weird connection problems to the TCP server.

First, here is some info from the TCP server API :

The message sequence of the protocol basically consists of commands sent from external devices / applications(hereinafter called clients), and response messages sent from Kairos. Every command and response message ends witha '\r\n' character sequence or also referred to as 0D 0A in ASCII
and a few examples :

image

There a few information I want to pull on a regular basis, so I trigger them with a inject node in repeat mode, and send both in the TCP request node :

TCP request flow , including keep alive :

The problem is, sometimes I get replies to both messages, but sometimes only from one, which also gets the msg.topic from the other inject node.... example ( I know for sure the remaintc value is coming from the CP timecode inject node) :

image

I played a lot with it, but oculd not find any way to handle that in a reliable way.

Would be very glad if anyone can help.

here are the two concerned flows :

TCP request flow :

[
    {
        "id": "7dfb1ca1aae955af",
        "type": "subflow",
        "name": "Kairos TCP Request",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 100,
                "y": 140,
                "wires": [
                    {
                        "id": "f606da2aa38c4ce6"
                    },
                    {
                        "id": "3b447fd41df4372d"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1560,
                "y": 100,
                "wires": [
                    {
                        "id": "79598df596cfbe05",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "69a9af0c4849ff43",
        "type": "comment",
        "z": "7dfb1ca1aae955af",
        "name": "in case of an active subscription, Kairos might send messages every 5 secondsbut the client needs to actively send keep-alive messages to keep the connection active.",
        "info": "",
        "x": 760,
        "y": 20,
        "wires": []
    },
    {
        "id": "a1c0c4a8a86d1c56",
        "type": "inject",
        "z": "7dfb1ca1aae955af",
        "name": "Initiates Keep-Alive",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "0.5",
        "topic": "keep-alive",
        "payload": "",
        "payloadType": "date",
        "x": 270,
        "y": 180,
        "wires": [
            [
                "f606da2aa38c4ce6"
            ]
        ]
    },
    {
        "id": "f606da2aa38c4ce6",
        "type": "trigger",
        "z": "7dfb1ca1aae955af",
        "name": "",
        "op1": "reset",
        "op2": "true",
        "op1type": "str",
        "op2type": "bool",
        "duration": "4",
        "extend": true,
        "overrideDelay": false,
        "units": "s",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 460,
        "y": 180,
        "wires": [
            [
                "2cc454b1022fe2fc"
            ]
        ]
    },
    {
        "id": "2cc454b1022fe2fc",
        "type": "trigger",
        "z": "7dfb1ca1aae955af",
        "name": "",
        "op1": "\\r\\n",
        "op2": "0",
        "op1type": "str",
        "op2type": "str",
        "duration": "-5",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "reset",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 620,
        "y": 180,
        "wires": [
            [
                "75b438864033b0c1"
            ]
        ]
    },
    {
        "id": "75b438864033b0c1",
        "type": "function",
        "z": "7dfb1ca1aae955af",
        "name": "msg = \"\\r\\n\"",
        "func": "msg.payload = '\\r\\n';\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 870,
        "y": 180,
        "wires": [
            [
                "070c7ad63dc32855",
                "480d95f2c3589be8"
            ]
        ]
    },
    {
        "id": "3b447fd41df4372d",
        "type": "function",
        "z": "7dfb1ca1aae955af",
        "name": "add \"\\r\\n\"",
        "func": "msg.payload = msg.payload + '\\r\\n';\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 860,
        "y": 140,
        "wires": [
            [
                "070c7ad63dc32855",
                "480d95f2c3589be8"
            ]
        ]
    },
    {
        "id": "070c7ad63dc32855",
        "type": "tcp request",
        "z": "7dfb1ca1aae955af",
        "name": "Kairos SimpleProt-3005",
        "server": "10.70.240.51",
        "port": "3005",
        "out": "char",
        "ret": "string",
        "splitc": "\\n",
        "newline": "",
        "trim": false,
        "tls": "",
        "x": 1170,
        "y": 140,
        "wires": [
            [
                "79598df596cfbe05"
            ]
        ]
    },
    {
        "id": "79598df596cfbe05",
        "type": "function",
        "z": "7dfb1ca1aae955af",
        "name": "Raw text to json ",
        "func": "var data1 = [];\n\n// remove addiitonal \\r\\n caracters in CP list: \nif (msg.topic == \"listCP\") {\n\ndata1 = msg.payload.substr(0,msg.payload.length-4);\n}\n\nelse {\ndata1 = msg.payload.split(\"\\r\\n\");\n};\n\nmsg.payload = data1;\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1420,
        "y": 100,
        "wires": [
            []
        ]
    },
    {
        "id": "480d95f2c3589be8",
        "type": "tcp out",
        "z": "7dfb1ca1aae955af",
        "d": true,
        "name": "",
        "host": "10.70.240.51",
        "port": "3005",
        "beserver": "client",
        "base64": false,
        "end": true,
        "tls": "",
        "x": 1160,
        "y": 220,
        "wires": []
    },
    {
        "id": "f317d45c9d294aea",
        "type": "tcp in",
        "z": "7dfb1ca1aae955af",
        "d": true,
        "name": "",
        "server": "server",
        "host": "",
        "port": "3005",
        "datamode": "stream",
        "datatype": "utf8",
        "newline": "",
        "topic": "",
        "trim": false,
        "base64": false,
        "tls": "",
        "x": 1200,
        "y": 260,
        "wires": [
            [
                "79598df596cfbe05"
            ]
        ]
    }
]

Thanks in advance !

Jerome

Will queuing the inputs and only allowing new input once tcp responds work

[{"id":"7f219818a234a3c6","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"eeeeee","payloadType":"str","x":510,"y":520,"wires":[["3b447fd41df4372d"]]},{"id":"3b447fd41df4372d","type":"function","z":"d1395164b4eec73e","name":"add \"\\r\\n\"","func":"msg.payload = msg.payload + '\\r\\n';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":520,"wires":[["34183a5dc8964902"]]},{"id":"34183a5dc8964902","type":"delay","z":"d1395164b4eec73e","name":"queue","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"60","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":790,"y":520,"wires":[["070c7ad63dc32855"]]},{"id":"9064a9d7a789745c","type":"function","z":"d1395164b4eec73e","name":"function 53","func":"msg = {flush: 1};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1030,"y":420,"wires":[["34183a5dc8964902"]]},{"id":"070c7ad63dc32855","type":"tcp request","z":"d1395164b4eec73e","name":"Kairos SimpleProt-3005","server":"localhost","port":"3005","out":"char","ret":"string","splitc":"\\n","newline":"","trim":false,"tls":"","x":1000,"y":520,"wires":[["79598df596cfbe05","9064a9d7a789745c"]]},{"id":"79598df596cfbe05","type":"function","z":"d1395164b4eec73e","name":"Raw text to json ","func":"var data1 = [];\n\n// remove addiitonal \\r\\n caracters in CP list: \nif (msg.topic == \"listCP\") {\n\ndata1 = msg.payload.substr(0,msg.payload.length-4);\n}\n\nelse {\ndata1 = msg.payload.split(\"\\r\\n\");\n};\n\nmsg.payload = data1;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1250,"y":480,"wires":[["c4ed1a234de9cfc6"]]},{"id":"c4ed1a234de9cfc6","type":"debug","z":"d1395164b4eec73e","name":"debug 342","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":580,"wires":[]},{"id":"2e80c8be5fec29f9","type":"tcp in","z":"d1395164b4eec73e","name":"","server":"server","host":"10.70.240.51","port":"3005","datamode":"stream","datatype":"utf8","newline":"\\r\\n","topic":"","trim":true,"base64":false,"tls":"","x":570,"y":600,"wires":[["3ad5731de3d9aa2b"]]}]

Thank you very much, will give it a try

Works like a charm !

image

Thank you so much

PS : do you think that might introduce some kind of delay in the communication ? this example is 1 per second but I might something a bit faster for other commands..

It will be as fast as the tcp client responds. Maybe someone else will have a way that can send all request at once, but I think we would need more info on the client API.

You maybe able to speed it up with multiple TCP request nodes and spreading the load.

When you say :

You mean the server API ? unfortunately the available documentation does not give much more information.

Many thanks for your help anyway.

This kind of help makes me want to advice other people as well....

Cheers

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