# Connect to a TCP Server with special packet order

**URL:** https://discourse.nodered.org/t/connect-to-a-tcp-server-with-special-packet-order/82162
**Category:** General
**Tags:** function-node
**Created:** [18 October 2023 14:40 UTC](https://discourse.nodered.org/t/connect-to-a-tcp-server-with-special-packet-order/82162 "2023-10-18T14:40:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 October 2023 14:40 UTC](https://discourse.nodered.org/t/connect-to-a-tcp-server-with-special-packet-order/82162/1 "2023-10-18T14:40:06Z")

</div>

Hi everyone!

I need to connect my Node-RED Instance to a TCP Server in a special way. The packet is described as follows:

#### Description of the packet

- 4 first bytes : the magic number 0xDEC0DA6E stored in little endian (lower byte first)
- 4 next bytes : the length of the data in bytes stored in little endian (lower byte first)
- the content : a JSON tree in UTF8

The JSON tree is no problem to create, but does anybody know how to get these additional bytes combined into the **msg.payload** to send out via the TCP Nodes?

Thanks for your help!  
Florian

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [18 October 2023 14:57 UTC](https://discourse.nodered.org/t/connect-to-a-tcp-server-with-special-packet-order/82162/2 "2023-10-18T14:57:32Z")

</div>

Hi @FloRu

Something like this _may_ work.

I should note: I'm not good in this area,

```auto
// Assuming msg.payload is your JSON object

// Header
const HeaderBytes = Buffer.from("0xDEC0DA6E","hex")

// Length
const Str = JSON.stringify(msg.payload)
const SizeBytes = Buffer.allocUnsafe(4);
SizeBytes.writeInt32LE(Str.length);

// Data
const DataBytes = Buffer.from(Str, "utf-8");

msg.payload = Buffer.concat([HeaderBytes,SizeBytes,DataBytes])

return msg

```

**EDIT**  
doesn't seem to work - wait for someone who knows what they are doing 😄

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [18 October 2023 15:59 UTC](https://discourse.nodered.org/t/connect-to-a-tcp-server-with-special-packet-order/82162/3 "2023-10-18T15:59:01Z")

</div>

> [@marcus-j-davies](#):
>
> ```auto
> // Assuming msg.payload is your JSON object
> 
> // Header
> const HeaderBytes = Buffer.from("0xDEC0DA6E","hex")
> 
> // Length
> const Str = JSON.stringify(msg.payload)
> const SizeBytes = Buffer.allocUnsafe(4);
> SizeBytes.writeInt32LE(Str.length);
> 
> // Data
> const DataBytes = Buffer.from(Str, "utf-8");
> 
> msg.payload = Buffer.concat([HeaderBytes,SizeBytes,DataBytes])
> 
> return msg
> 
> ```

I deleted the "0x" from the HeaderBytes so that the raw payload seems to be correct, but don't get any connection. _[EDIT]: ...and - of course - changed the order of the Header Bytes because of the "lower byte first"..._  
Also the SizeBytes seems to be right - the first byte changes if I give one more character into the payload. But most probably there's some information missing for them to work.

I'll give a short feedback as soon as I managed to get the connection done!

Thanks!  
Best,  
Florian

---

<div class="post-metadata">

### Author: ![FloRu](https://avatars.discourse-cdn.com/v4/letter/f/db5fbb/32.png) [@FloRu](https://discourse.nodered.org/u/FloRu)
#### Post date: [20 October 2023 09:58 UTC](https://discourse.nodered.org/t/connect-to-a-tcp-server-with-special-packet-order/82162/4 "2023-10-20T09:58:53Z")

</div>

Here's the final Code:

```auto
// Header
const HeaderBytes = Buffer.from("6EDAC0DE", "hex");

// Length
const Str = JSON.stringify(msgOut);
const SizeBytes = Buffer.allocUnsafe(4);
SizeBytes.writeInt32LE(Str.length);

// Data
let DataBytes = Buffer.from(Str, "utf-8");

msg.payload = Buffer.concat([HeaderBytes, SizeBytes, DataBytes]);

```

Thanks for your help!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [3 November 2023 09:59 UTC](https://discourse.nodered.org/t/connect-to-a-tcp-server-with-special-packet-order/82162/5 "2023-11-03T09:59:13Z")

</div>

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