Extract _Session

Hi, I'm trying to extract the msg._session property from Msg.payload1 and combine it with a MQTT message (Msg.payload 2) to direct the MQTT messgae to the origin TCP port. I've found that using the MQTT node changes the message properties so i loose the "_session" id.

Basically im using legacy software that uses TCP/IP and i would like to "publish" the data send on the TCP port to my MQTT broker (Works a treat). This issue occurs when i have more than one port in use because without a "msg._session" property the reply on the TCP node is sent to all clients!

So i need to add the "msg._session" data from the TCP listening node and combine it with the MQTT node to direct the received data o the correct port.

image

[{"id":"711e43ea.656e5c","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"151e34e1.25016b","type":"debug","z":"711e43ea.656e5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":970,"y":200,"wires":[]},{"id":"ac52cdac.04c0c","type":"inject","z":"711e43ea.656e5c","name":"Msg.payload 1","topic":"","payload":"{\"topic\":\"\",\"payload\":[47,63,48,48,49,33,13,10],\"ip\":\"::ffff:**.**.***.***\",\"port\":50584,\"_session\":{\"type\":\"tcp\",\"id\":\"10ba3c3c.801b74\"},\"_msgid\":\"d9b8325f.30c5e\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":180,"wires":[["fd9418ad.dd1628"]]},{"id":"fd9418ad.dd1628","type":"change","z":"711e43ea.656e5c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload._session\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":180,"wires":[["c819d715.18bc38"]]},{"id":"278b7adf.cb5d76","type":"inject","z":"711e43ea.656e5c","name":"Msg.payload 2","topic":"","payload":"{\"topic\":\"Elster_A1700\",\"payload\":[6],\"qos\":0,\"retain\":false,\"_msgid\":\"52bd4fe6.ab172\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":320,"y":240,"wires":[["c819d715.18bc38"]]},{"id":"c819d715.18bc38","type":"join","z":"711e43ea.656e5c","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"/n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":830,"y":200,"wires":[["151e34e1.25016b"]]}]

I can extract the message using the change node and join it with the MQTT message, when i test it i keep getting "Cant merge non-object types"
image

Any help would be greatly appreciated!

First things first, your flow is not importable. Please read How to share code or flow json and edit your first message (with a fresh export as the forum software messed it up in the current form).

If I can understand it well, you have 2 incoming nodes that you like to merge together. Have you looked at the join set to merge on topics and made sure that both incoming messages have a different topic?

Thanks @afelix! you should be able to import the flow.

I've tried the set and join node, i can extract the msg._session details but when i try to combine it with the second payload i get the error message.
When the data passes through the TCP node to the MQTT node it looses the msg._session detail so the reply is send to all clients.
image
My flow looks like this:
SEND DATA: TCP Listening node (Portxxx) >>>>>>>MQTT Out>>>>Serial device
RECEIVE DATA: Serial device>>>>>>MQTT In>>>>>TCP "Reply" node
What i need to do is extract the msg._session from the TCP node and combine it with the MQTT in payload

There is no good way to do this - how do you expect the serial device to hold onto the session data ?
The best you can do is to save the _session into a flow context variable - and then reattach it afterwards - but you will also need to add logic to also keep hold of a sequence number for each message - and count them out and back in again - as, unless you can be certain that you only have one message passing through at a time, if a second arrives it could overwrite any context you had saved.

Thanks for you quick response @dceejay!
We have old legacy devices that use serial communications and were trying to bring the communications into the 21st century! We "Pull" the data from the serial device using propitiatory protocol (Transmit/receive) and its one message at a time. The software gives me the option of TCP/IP or Serial comms to pull the data.
I'm new to using Node-red and had an idea of using MQTT to communicate with the remote serial device as a proof of concept. I set everything up and it works a treat! (To my surprise) when i use one TCP/IP port. As soon as i add a second port i get mixed messages...I feel like i'm close to finding the solution!

i'm slowly learning as i go along and now understand that the message proprieties change between nodes which means i need to extract the msg._session before it gets to the serial device and combine it with the response from the serial device.

We have one message at a time unless we experience latency issues.

Right i will look into saving the session into a flow context variable.

Found the solution!
I used the change node to extract the TCP session data (SET) , i then used a second change node to set the msg.complete property to any messages received. I then finish with a Join node to the TCP "Reply" configured.
SEND DATA: TCP Listening node (Portxxx) >>>>(Change node SET)>>>MQTT Out>>>>Serial device
RECEIVE DATA: Serial device>>>>>>MQTT In>>(Change node SET msg.complete)>>(Join)>TCP "Reply" node

1 Like