# Sending general files, json files, images over MQTT between different Node-Red devices

**URL:** <https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512>\
**Category:** General\
**Created:** [23 September 2023 08:44 UTC](https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512 "2023-09-23T08:44:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_cave\_dweller](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_cave\_dweller](https://discourse.nodered.org/u/dr_cave_dweller)\
**Post date:** [23 September 2023 08:44 UTC](https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512/1 "2023-09-23T08:44:27Z")

</div>

Is this possible to send files over MQTT using the Node-Red MQTT nodes?

I have tried using FTP to send files between different devices running Node-Red on same network but can't get it working.

I however can send data over MQTT without drama. I can send a basic text file over MQTT, but items like a .jpg file are not opening on the other end.

Any advice appreciated.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 September 2023 09:03 UTC](https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512/2 "2023-09-23T09:03:48Z")

</div>

A jpeg is not text, as you have discovered.  
You would read the file and output a buffer, you then send the buffer over mqtt. You may want to send filename (where you want to save and name this file) also, so you would move things into payload to send, and move them back to msg properties to save.  
here is an example assuming both machines have same folder directory.

```auto
[{"id":"f61b661169542bd7","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"filename","v":"/data/data/com.termux/files/home/.node-red/static/files/test.jpg","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":360,"wires":[["ec6ef0512f78d97d"]]},{"id":"ec6ef0512f78d97d","type":"file in","z":"b9860b4b9de8c8da","name":"","filename":"filename","filenameType":"msg","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":420,"y":360,"wires":[["0f6be3e427a24f98"]]},{"id":"0f6be3e427a24f98","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.data","tot":"msg"},{"t":"set","p":"payload.filename","pt":"msg","to":"filename","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":360,"wires":[["df1432381b72c116"]]},{"id":"df1432381b72c116","type":"mqtt out","z":"b9860b4b9de8c8da","name":"","topic":"my/file/topic","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e8ba3ef5.22f4a8","x":810,"y":360,"wires":[]},{"id":"a4dc60a831051397","type":"mqtt in","z":"b9860b4b9de8c8da","name":"","topic":"my/file/topic","qos":"2","datatype":"auto-detect","broker":"e8ba3ef5.22f4a8","nl":false,"rap":true,"rh":0,"inputs":0,"x":388.0000114440918,"y":416.0000047683716,"wires":[["241be0cb3fde7568"]]},{"id":"241be0cb3fde7568","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"payload.filename","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":578.0000114440918,"y":416.0000047683716,"wires":[["111b1952a7ffcb1f","8f90a19e23210d78"]]},{"id":"8f90a19e23210d78","type":"function","z":"b9860b4b9de8c8da","name":"function 22","func":"msg.payload = Buffer.from(msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":460,"wires":[["6954ea05bcf9b9f6"]]},{"id":"111b1952a7ffcb1f","type":"debug","z":"b9860b4b9de8c8da","name":"debug 339","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":440,"wires":[]},{"id":"6954ea05bcf9b9f6","type":"file","z":"b9860b4b9de8c8da","name":"","filename":"filename","filenameType":"msg","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":800,"y":520,"wires":[["206ef230aa8a551e"]]},{"id":"e8ba3ef5.22f4a8","type":"mqtt-broker","name":"testb","broker":"192.168.1.25","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

[edit] fixed write file to overwrite, not append.  
Also fixed converting the data back to a buffer .

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [23 September 2023 10:25 UTC](https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512/3 "2023-09-23T10:25:44Z")

</div>

I use this MQTT technique to send messages from one NR on one machine to another NR on another machine

I have two function nodes with a bit of JavaScript in them to let me cleanly send/receive messages between them

```auto
delete msg['_msgid'];
return ({ "payload": msg });

```

for sending

```auto
var data;
data = (JSON.parse((msg['payload'])));
return data;

```

for receiving

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 September 2023 10:47 UTC](https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512/4 "2023-09-23T10:47:23Z")

</div>

A couple of points to save you typing in the future

> [@cymplecy](#):
>
> `return ({ "payload": msg });`

That could be  
`return { payload: msg };`  
You don't need the quotes unless the key has special characters in it, and the parentheses are not required.

> [@cymplecy](#):
>
> `data = (JSON.parse((msg['payload'])));`

Similarly that could be  
`data = JSON.parse(msg.payload);`

Or the whole thing could just be  
`return JSON.parse(msg.payload);`

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [23 September 2023 10:58 UTC](https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512/5 "2023-09-23T10:58:14Z")

</div>

I actually use Blockly - this is just the JS it produces 🙂

---

<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:** [22 November 2023 10:58 UTC](https://discourse.nodered.org/t/sending-general-files-json-files-images-over-mqtt-between-different-node-red-devices/81512/6 "2023-11-22T10:58:27Z")

</div>

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