# Loadbalance/toggle messages

**URL:** <https://discourse.nodered.org/t/loadbalance-toggle-messages/45514>\
**Category:** General\
**Created:** [11 May 2021 20:52 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514 "2021-05-11T20:52:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [11 May 2021 20:52 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/1 "2021-05-11T20:52:58Z")

</div>

Hi, I would like every incoming message (from HTTP listner) to alternate routes in my flow. For example

- first message -\> direction A
- seconde message -\> direction B
- third message -\> direction A  
etc

In other words i want each message toggles between route A and B  
What is the best way to do this?

Hopefully someone can help me with this

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 May 2021 20:57 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/2 "2021-05-11T20:57:02Z")

</div>

Use [shared subscriptions](https://www.hivemq.com/blog/mqtt5-essentials-part7-shared-subscriptions/)...

![09BMsbEczg](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3d0eb594511fe00b9038ca467817620d90b50cee.gif)

Demo...

```auto
[{"id":"411619e2.a2bf58","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"$share/group1/test/data","qos":"2","datatype":"auto","broker":"f03246cf.89b378","x":200,"y":760,"wires":[["eb372c90.73c19"]]},{"id":"93d1c977.a877e8","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"$share/group1/test/data","qos":"2","datatype":"auto","broker":"9bf6dfdb.d9a34","x":200,"y":820,"wires":[["17632783.2e9cd8"]]},{"id":"e6cd58c5.c04f48","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"$share/group1/test/data","qos":"2","datatype":"auto","broker":"7c31b1dc.6809f","x":200,"y":880,"wires":[["53cf5244.88900c"]]},{"id":"eb372c90.73c19","type":"debug","z":"4b3f21a3.ba434","name":"lh1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":410,"y":760,"wires":[]},{"id":"17632783.2e9cd8","type":"debug","z":"4b3f21a3.ba434","name":"lh2","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":410,"y":820,"wires":[]},{"id":"53cf5244.88900c","type":"debug","z":"4b3f21a3.ba434","name":"lh3","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":410,"y":880,"wires":[]},{"id":"e895883b.fa6008","type":"mqtt out","z":"4b3f21a3.ba434","name":"","topic":"test/data","qos":"1","retain":"false","broker":"f03246cf.89b378","x":420,"y":940,"wires":[]},{"id":"e99da80a.d34378","type":"inject","z":"4b3f21a3.ba434","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":940,"wires":[["e895883b.fa6008"]]},{"id":"f03246cf.89b378","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"5","keepalive":"55","cleansession":true,"birthTopic":"demo/lwt/birth","birthQos":"0","birthPayload":"birth","birthMsg":{},"closeTopic":"demo/lwt/close","closeQos":"0","closePayload":"close","closeMsg":{},"willTopic":"demo/lwt/will","willQos":"0","willPayload":"will","willMsg":{},"sessionExpiry":""},{"id":"9bf6dfdb.d9a34","type":"mqtt-broker","name":"lh2","broker":"localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"7c31b1dc.6809f","type":"mqtt-broker","name":"lh3","broker":"localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [11 May 2021 21:07 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/3 "2021-05-11T21:07:51Z")

</div>

Thanks @Steve-Mcl a dont use mqtt listener but HTTP. i have this flow

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b8917857ff8197c9f9c867509aca7d50f99e6d8.jpeg)

`[{"id":"47201782.a0f8e8","type":"http response","z":"fa16c90b.a845f8","name":"http response","statusCode":"","headers":{},"x":840,"y":280,"wires":[]},{"id":"ab5a8b00.b16f28","type":"change","z":"fa16c90b.a845f8","name":"SET A","rules":[{"t":"set","p":"payload","pt":"msg","to":"A","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":260,"wires":[["47201782.a0f8e8"]]},{"id":"fa3bb62c.887db8","type":"change","z":"fa16c90b.a845f8","name":"SET B","rules":[{"t":"set","p":"payload","pt":"msg","to":"B","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":320,"wires":[["47201782.a0f8e8"]]},{"id":"894533b0.bf06f","type":"http in","z":"fa16c90b.a845f8","name":"","url":"/loadbalance","method":"get","upload":false,"swaggerDoc":"","x":200,"y":280,"wires":[[]]},{"id":"242c20af.810d3","type":"comment","z":"fa16c90b.a845f8","name":"TOGGLE HERE?","info":"?","x":420,"y":280,"wires":[]}]`

Instead of "toggle here" there should be something that ensures that there is alternation

---

<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:** [11 May 2021 21:09 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/4 "2021-05-11T21:09:58Z")

</div>

Here is a simple example of a toggle route.

```auto
[{"id":"26247786.436368","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":560,"wires":[["8d0d7063.cc8e2"]]},{"id":"8d0d7063.cc8e2","type":"change","z":"9b3f9f31.c45298","name":"","rules":[{"t":"set","p":"alternate","pt":"flow","to":"$flowContext(\"alternate\") ? false : true","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":560,"wires":[["a89d4dfb.ea976"]]},{"id":"a89d4dfb.ea976","type":"switch","z":"9b3f9f31.c45298","name":"","property":"alternate","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":540,"y":560,"wires":[["9a5e7511.8c4ee"],["2a9ccdae.1f5102"]]},{"id":"9a5e7511.8c4ee","type":"debug","z":"9b3f9f31.c45298","name":"one","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":540,"wires":[]},{"id":"2a9ccdae.1f5102","type":"debug","z":"9b3f9f31.c45298","name":"two","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":600,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 May 2021 21:11 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/5 "2021-05-11T21:11:19Z")

</div>

> [@coen17st](#):
>
> Thanks @Steve-Mcl a dont use mqtt listener BUT HTTP.

Ahh, i miss-read. Sorry.

You could use a simple function to toggle it...

![JR2PpBxBnV](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/a/0a7944aeeccd8afa89891104d6ece4bdb693f09d.gif)

```auto
[{"id":"bee04cd.ec4e0b","type":"function","z":"4b3f21a3.ba434","name":"toggle","func":"var toggle = context.get(\"toggle\");\nif (toggle === true) {\n toggle = false;\n} else {\n toggle = true;\n}\ncontext.set(\"toggle\", toggle);\n\nif(toggle) {\n return [msg, null];\n} else {\n return [null, msg];\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1110,"y":1080,"wires":[["155bc2ad.c53ccd"],["d280d068.5c63b"]]},{"id":"514ac66f.97e6d8","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":950,"y":1080,"wires":[["bee04cd.ec4e0b"]]},{"id":"155bc2ad.c53ccd","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1310,"y":1040,"wires":[]},{"id":"d280d068.5c63b","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1310,"y":1120,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [11 May 2021 21:17 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/6 "2021-05-11T21:17:54Z")

</div>

Thanks a lot! 😄

although I don't really understand how the code in the function node works but I'm going to find out. Thanks for the solution!

---

<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:** [12 May 2021 06:12 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/7 "2021-05-12T06:12:04Z")

</div>

Out of interest, what activity is there in the two routes (before they merge again) that will benefit from the load sharing?

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [12 May 2021 06:28 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/8 "2021-05-12T06:28:12Z")

</div>

Hi @coen17st ,  
You could also use the "round robin" option in my [node-red-contrib-msg-router](https://github.com/bartbutenaers/node-red-contrib-msg-router) node.  
Bart

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 May 2021 08:22 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/9 "2021-05-12T08:22:16Z")

</div>

As long as you toggle messages to alternate nodes in the _same flow_ like that I doubt you will load balance anything (unless some of the following nodes in your flow are executing external processes). To load balance NR I would have two separate instances running in parallel or having them running on separate machines and then toggle the messages to each of them

---

<div class="post-metadata">

**Author:** ![coen17st](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/coen17st/32/34027_2.png) [@coen17st](https://discourse.nodered.org/u/coen17st)\
**Post date:** [14 May 2021 14:54 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/10 "2021-05-14T14:54:02Z")

</div>

Without going into very detail, i am using this for loadbalacing two external servers and not the server where node-red is running on. Its doing a HTTP call and node-red replys which server it must be (A or B)

---

<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:** [14 May 2021 15:25 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/11 "2021-05-14T15:25:55Z")

</div>

> [@coen17st](#):
>
> node-red replys which server it must be

If you don't need to route within node red, but just want an alternating result then it is even easier. All you need is a function containing

```auto
const options = ["Server A", "Server B"]
let option = context.get("option") || 0
option = (option + 1) & 1
msg.payload = options[option]
context.set("option", option)
return msg;

```

I predict you will now get another three suggestions on even more concise ways of coding that. 🙂

---

<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:** [28 May 2021 15:26 UTC](https://discourse.nodered.org/t/loadbalance-toggle-messages/45514/12 "2021-05-28T15:26:36Z")

</div>

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