Skip to content

Instantly share code, notes, and snippets.

@mharizanov
Created November 6, 2014 13:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharizanov/8ba7f90f3ea8d92b1e01 to your computer and use it in GitHub Desktop.
Save mharizanov/8ba7f90f3ea8d92b1e01 to your computer and use it in GitHub Desktop.
Wait for and combine multiple inputs to a node

I sometimes need to have multiple inputs to a node, however due to the asynchronous nature of node-red flows, this isn't quite trivial. Here is a template that can help solve this issue

[{"id":"715ae02.ffcb82","type":"function","name":"Wait for all tasks to finish","func":"context.data = context.data || new Object();\n\nswitch (msg.topic) {\n case \"task1\":\n context.data.task1 = msg.payload;\n msg = null;\n break;\n case \"task2\":\n context.data.task2 = msg.payload;\n msg = null;\n break;\n case \"task3\":\n context.data.task3 = msg.payload;\n msg = null;\n break;\n \n default:\n msg = null;\n \tbreak;\n\n}\n\nif(context.data.task1 != null && context.data.task2 != null && context.data.task3 != null) {\n\tmsg2 = new Object();\n msg2 = context.data;\n context.data=null;\n\treturn msg2;\n} else return msg;","outputs":1,"x":695,"y":212,"z":"4d5a619c.fa15f","wires":[["1db3a013.cd5468"]]},{"id":"33b2850d.f24e6a","type":"delay","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":437,"y":121,"z":"4d5a619c.fa15f","wires":[["715ae02.ffcb82"]]},{"id":"1db3a013.cd5468","type":"debug","name":"","active":true,"console":"false","complete":"true","x":902,"y":211,"z":"4d5a619c.fa15f","wires":[]},{"id":"35eb6e77.0f77da","type":"delay","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":441,"y":217,"z":"4d5a619c.fa15f","wires":[["715ae02.ffcb82"]]},{"id":"c8ab7682.98ada","type":"delay","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":438,"y":301,"z":"4d5a619c.fa15f","wires":[["715ae02.ffcb82"]]},{"id":"1ccffde6.02aa52","type":"function","name":"Task1","func":"msg.topic=\"task1\";\nmsg.payload=\"Task1's payload\"\nreturn msg;","outputs":1,"x":269,"y":121,"z":"4d5a619c.fa15f","wires":[["33b2850d.f24e6a"]]},{"id":"fe4cbb39.4f93c8","type":"function","name":"Task2","func":"msg.topic=\"task2\";\nmsg.payload=\"Task2's payload\"\nreturn msg;","outputs":1,"x":272,"y":216,"z":"4d5a619c.fa15f","wires":[["35eb6e77.0f77da"]]},{"id":"c3766a5d.afab88","type":"function","name":"Task3","func":"msg.topic=\"task3\";\nmsg.payload=\"Task3's payload\"\nreturn msg;","outputs":1,"x":267,"y":300,"z":"4d5a619c.fa15f","wires":[["c8ab7682.98ada"]]},{"id":"2dc185ba.57852a","type":"inject","name":"Start","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":103,"y":205,"z":"4d5a619c.fa15f","wires":[["1ccffde6.02aa52","fe4cbb39.4f93c8","c3766a5d.afab88"]]}]
@mfeblowitz
Copy link

This pattern appears to assume that only one instance of each task will be arriving. Is there a means of blocking subsequent instances of, say, task 2 results, from overwriting prior instances?

@MattiasMM
Copy link

Alternative flow that gives each incoming message a unique ID, and that checks for that ID at the join node.
[{"id":"bdb52f32.3c0e4","type":"function","z":"d3334211.d1d0c8","name":"Wait for all tasks to finish","func":"QueryID = msg.ID;\ncontext.data = context.data || {};\ncontext.data[QueryID] = context.data[QueryID] || {task1 : null, task2: null, task3: null};\n\nswitch (msg.topic) {\n case "task1":\n context.data[QueryID].task1 = msg.payload;\n msg = null;\n break;\n case "task2":\n context.data[QueryID].task2 = msg.payload;\n msg = null;\n break;\n case "task3":\n context.data[QueryID].task3 = msg.payload;\n msg = null;\n break;\n \n default:\n msg = null;\n \tbreak;\n\n}\n\nif(context.data[QueryID].task1 !== null && context.data[QueryID].task2 !== null && context.data[QueryID].task3 !== null) {\n\tmsg2 = {};\n msg2 = context.data[QueryID];\n delete context.data[QueryID];\n\treturn msg2;\n} else return msg;","outputs":1,"noerr":0,"x":777,"y":1403,"wires":[["a2f95d9.d67ada"]]},{"id":"a501db75.5b02e","type":"delay","z":"d3334211.d1d0c8","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":519,"y":1312,"wires":[["bdb52f32.3c0e4"]]},{"id":"a2f95d9.d67ada","type":"debug","z":"d3334211.d1d0c8","name":"","active":true,"console":"false","complete":"true","x":984,"y":1402,"wires":[]},{"id":"620c74c9.0b758c","type":"delay","z":"d3334211.d1d0c8","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":523,"y":1408,"wires":[["bdb52f32.3c0e4"]]},{"id":"aabee8b4.2422d8","type":"delay","z":"d3334211.d1d0c8","name":"Random delay","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":1492,"wires":[["bdb52f32.3c0e4"]]},{"id":"166aae9.07a3651","type":"function","z":"d3334211.d1d0c8","name":"Task1","func":"msg.ID = msg.payload; //a unique ID given by the creator of this message.\nmsg.topic="task1";\nmsg.payload="Task1's payload"\nreturn msg;","outputs":1,"noerr":0,"x":351,"y":1312,"wires":[["a501db75.5b02e"]]},{"id":"3d8ee315.fafcdc","type":"function","z":"d3334211.d1d0c8","name":"Task2","func":"msg.ID = msg.payload; //a unique ID given by the creator of this message.\nmsg.topic="task2";\nmsg.payload="Task2's payload"\nreturn msg;","outputs":1,"noerr":0,"x":354,"y":1407,"wires":[["620c74c9.0b758c"]]},{"id":"f6e54c8e.12128","type":"function","z":"d3334211.d1d0c8","name":"Task3","func":"msg.ID = msg.payload; //a unique ID given by the creator of this message.\nmsg.topic="task3";\nmsg.payload="Task3's payload"\nreturn msg;","outputs":1,"noerr":0,"x":349,"y":1491,"wires":[["aabee8b4.2422d8"]]},{"id":"8db3e0ae.203a48","type":"inject","z":"d3334211.d1d0c8","name":"Start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":185,"y":1396,"wires":[["166aae9.07a3651","3d8ee315.fafcdc","f6e54c8e.12128"]]}]

@KirillDrozdov
Copy link

Hi, Matias, I can't export your flow, could please check it?

@Diez66
Copy link

Diez66 commented Apr 4, 2020

Problem solved a long time ago, for me, it was a new problem today and you solved it for me, Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment