# \[Fun Exercise\]Make an MQTT broker in Node-RED

**URL:** <https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229>\
**Category:** General\
**Created:** [21 October 2023 08:43 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229 "2023-10-21T08:43:31Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [23 October 2023 14:27 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/21 "2023-10-23T14:27:41Z")

</div>

> [@cymplecy](#):
>
> The node-red-contrib-aedes

I use aedes - but I'm intrigued to see how this goes - hence my excitement at the start.

---

<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 October 2023 14:28 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/22 "2023-10-23T14:28:14Z")

</div>

It really is just for the fun of doing it 🙂

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [23 October 2023 14:30 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/23 "2023-10-23T14:30:05Z")

</div>

> [@cymplecy](#):
>
> node-red-contrib-aedes

I was just looking at that, or rather the aedes.js project - good to know that there is already an integration for Node-RED.

> [@cymplecy](#):
>
> It really is just for the fun of doing it 🙂

I also found the [mqtt standard](https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html) - I bet the butler did it in the end!

Thanks for the inspiration 👍

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [23 October 2023 14:46 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/24 "2023-10-23T14:46:33Z")

</div>

> [@gregorius](#):
>
> I bet the butler did it in the end!

Yeah looks like a nice easy read 😉

---

<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 October 2023 14:50 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/25 "2023-10-23T14:50:01Z")

</div>

~~Not~~ Note that aedes is missing a number of features such as Retained topics (unless it has been added recently). Mosquitto is so simple to install and setup, at least on Linux, that it really isn't worth using aedes.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [23 October 2023 15:06 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/26 "2023-10-23T15:06:40Z")

</div>

> [@Colin](#):
>
> Mosquitto is so simple to install and setup

Indeed, I installed in about 3mins on a raspberry pi but .... I had to go into a shell and terminal window.

I'm on the I-want-to-do-everything-in-node-red trip and therefore I really dislike leaving the soothing surroundings of nodes, events and wires! It would definitely only be for educational purposes.

Btw Aedes also took about 3mins to install but added 161 (sic!) additional packages to Node-RED!?!?

---

<div class="post-metadata">

**Author:** ![jmorris644](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jmorris644/32/31964_2.png) [@jmorris644](https://discourse.nodered.org/u/jmorris644)\
**Post date:** [23 October 2023 15:17 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/27 "2023-10-23T15:17:33Z")

</div>

Does anyone know if the Aedes got fixed? There was an error when you deployed.

---

<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:** [23 October 2023 15:28 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/28 "2023-10-23T15:28:02Z")

</div>

Well section 2 MQTT Control Packet format - is the main / most useful section (IMHO) - and (spoiler) no it wasn't the butler - it was [Andy S-C](https://en.wikipedia.org/wiki/Andy_Stanford-Clark), though our own @knolleary has implemented it a fair few times on various platforms 🙂

---

<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 October 2023 15:28 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/29 "2023-10-23T15:28:08Z")

</div>

FYI the Blockly code to read topic length and then extract its name.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5d436a8a12f54724fbe9a238269d34b8ae85ee6.png)

and the JS it produces for those of you who don't speak Blockly 🙂

```auto
var rcvdPayload, topicLength, topicName;

rcvdPayload = (msg['payload']);
topicLength = 256 * (rcvdPayload[5 - 1]) + (rcvdPayload[6 - 1]);
topicName = rcvdPayload.slice(6, 6 + topicLength);
topicName = (topicName.toString("utf8"));
msg['mqttPacket']['subscribe']['topicName'] = topicName;
node.status({fill:"blue", shape:"ring", text:([topicLength,':',topicName].join(''))});
return msg;

```

Note: Blockly is 1-based indexing as all good languages are......

---

<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:** [23 October 2023 15:30 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/30 "2023-10-23T15:30:47Z")

</div>

indeed Aedes does install everything including the kitchen sink... and of course is on the same thread as the rest of Node-RED (as node.js is (mostly) single threaded) so yeah uuurgh....

---

<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 October 2023 15:56 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/31 "2023-10-23T15:56:09Z")

</div>

One little issue that's come up is that although the switch node will accept non-decimal numbers, it highlights the box red as if they are invalid  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cb39ac03883263a2a4ab0daa2c963abfd7c8e1b1.png)

---

<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:** [23 October 2023 16:09 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/32 "2023-10-23T16:09:41Z")

</div>

probably worth raising as a real issue - at least for discussion - as it is probably the same across all numeric typed inputs.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [23 October 2023 16:13 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/33 "2023-10-23T16:13:10Z")

</div>

Wow! Blockly seems to be lego mindstorms for grown ups! I guess it would be fun project to reverse engineer the lego mindstorm protocol, and then use blockly in Node-RED to control lego mindstorm 🤔

> [@cymplecy](#):
>
> `topicLength = 256 * (rcvdPayload[5 - 1]) + (rcvdPayload[6 - 1]);`

It's weird how everything is bracket'ised but that line made me think back to [BODMAS](https://www.twinkl.de/teaching-wiki/bodmas) - there were I would have used a brackets, Blockly doesn't!

> [@dceejay](#):
>
> so yeah uuurgh....

Has been noted ... I created myself a [flow](https://flowhub.org/f/553ca20c0b35e191) to remind me of the technology.

---

<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 October 2023 16:21 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/34 "2023-10-23T16:21:02Z")

</div>

The transcoding from Blockly to JS is done in a very industrial way. 🙂  
Just needs to always work - doesn't need to look nice.

Never look how sausages are made 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 October 2023 20:15 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/35 "2023-10-23T20:15:11Z")

</div>

> [@marcus-j-davies](#):
>
> I use aedes

The main thing to note about that I think is that its performance is abysmal compared to the tiny service that is Mosquitto. This is not really its fault, just a consequence of doing low-level things in a high-level language I think.

Of course this may not matter to everyone. 🙂

---

<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 October 2023 20:15 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/36 "2023-10-23T20:15:54Z")

</div>

Wait till you see the performance of mine then 🙂

---

<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:** [23 October 2023 20:18 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/37 "2023-10-23T20:18:38Z")

</div>

I use aedes for interprocess on the same machine, I wouldn't dream of the practice for bigger projects that involve different hosts 😱

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [23 October 2023 20:40 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/38 "2023-10-23T20:40:13Z")

</div>

> [@dceejay](#):
>
> probably worth raising as a real issue - at least for discussion - as it is probably the same across all numeric typed inputs.

Don't do. There's already a PR posted, waiting to be approved, that addresses this "feature":

> <https://github.com/node-red/node-red/pull/4371>
>
> \- \[x\] Bugfix (non-breaking change which fixes an issue)
> \- \[x\] New feature (non-…breaking change which adds functionality)
> 
> \## Proposed changes
> 
> Based on a \[forum exchange\](https://discourse.nodered.org/t/set-number-value-to-nan-in-change-inject-node/81589), this PR extends the validity checks for typedInput fields of type 'number'.
> 
> The following additional formats will pass the check:
> 
> \- \`NaN\`
> \- Binary values, e.g.: \`0b10101\` or \`0B11101\`
> \- Octal values, e.g.: \`0o1357\` or \`0O2468\`
> \- Hex values, e.g.: 0x25aF or 0X69Cd
> 
> \## Checklist
> 
> 
> \- \[x\] I have read the \[contribution guidelines\](https://github.com/node-red/node-red/blob/master/CONTRIBUTING.md)
> \- \[x\] For non-bugfix PRs, I have discussed this change on the forum/slack team.
> \- \[\] I have run \`npm run test\` to verify the unit tests pass
> \- \[\] I have added suitable unit tests to cover the new/changed functionality

---

<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 October 2023 20:40 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/39 "2023-10-23T20:40:50Z")

</div>

I'll close the issue I raised

---

<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:** [23 October 2023 21:03 UTC](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229/40 "2023-10-23T21:03:44Z")

</div>

(Thanks for raising it though;-) )

[Previous page](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229.md?page=1)

[Next page](https://discourse.nodered.org/t/fun-exercise-make-an-mqtt-broker-in-node-red/82229.md?page=3)
