Loadbalance/toggle messages

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

Use shared subscriptions...

09BMsbEczg

Demo...

[{"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":""}]

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

[{"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

Here is a simple example of a toggle route.

[{"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":[]}]
1 Like

Ahh, i miss-read. Sorry.

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

JR2PpBxBnV

[{"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":[]}]
2 Likes

Thanks a lot! :smile:

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!

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

Hi @coen17st ,
You could also use the "round robin" option in my node-red-contrib-msg-router node.
Bart

3 Likes

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

1 Like

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)

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

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. :slight_smile:

3 Likes

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