# Extract \_Session

**URL:** https://discourse.nodered.org/t/extract-session/19311
**Category:** General
**Created:** [18 December 2019 16:55 UTC](https://discourse.nodered.org/t/extract-session/19311 "2019-12-18T16:55:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)
#### Post date: [18 December 2019 16:55 UTC](https://discourse.nodered.org/t/extract-session/19311/1 "2019-12-18T16:55:54Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a0b550fde7621d5f21dda063e40626fc35f2aaac.png)

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2a1aab51ff9434de85b4d959b8aea1cfa59f71a6.png)

Any help would be greatly appreciated!

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [18 December 2019 17:06 UTC](https://discourse.nodered.org/t/extract-session/19311/2 "2019-12-18T17:06:31Z")

</div>

First things first, your flow is not importable. Please read [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) 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?

---

<div class="post-metadata">

### Author: ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)
#### Post date: [19 December 2019 09:18 UTC](https://discourse.nodered.org/t/extract-session/19311/3 "2019-12-19T09:18:29Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/cd142c33fa070bd6110651f444482e36efb75124.png)  
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

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [19 December 2019 09:27 UTC](https://discourse.nodered.org/t/extract-session/19311/4 "2019-12-19T09:27:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)
#### Post date: [19 December 2019 10:04 UTC](https://discourse.nodered.org/t/extract-session/19311/5 "2019-12-19T10:04:27Z")

</div>

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!

> [@dceejay](#):
>
> There is no good way to do this - how do you expect the serial device to hold onto the session data ?

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.

> [@dceejay](#):
>
> unless you can be certain that you only have one message passing through at a time

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.

---

<div class="post-metadata">

### Author: ![New2Node](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@New2Node](https://discourse.nodered.org/u/New2Node)
#### Post date: [19 December 2019 13:23 UTC](https://discourse.nodered.org/t/extract-session/19311/6 "2019-12-19T13:23:59Z")

</div>

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
