# How to synchronize two input messages?

**URL:** <https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190>\
**Category:** General\
**Created:** [18 September 2021 07:47 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190 "2021-09-18T07:47:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [18 September 2021 07:47 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/1 "2021-09-18T07:47:15Z")

</div>

![Screen Shot 2021-09-18 at 15.18.32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/daab94bf53cad4d106bb25d3f3238fbf6fc3de37.png)

Using the screenshot I'm trying to popup a message box using a node which accepts

- the title of the message box
- the message to show

Here is the node function

```auto
function collectInputs(msg, context) {
	var title = context.get('title') || 'default title';
	var message = context.get('message') || 'default message';
	switch (msg.topic) {
		case 'title':
			title = msg.payload;
			context.set('title', title);
			break;
		case 'message':
            title = msg.payload;
            context.set('message', message);
            break;
	}
	let inputs = [
		{payload: title, topic: title},
		{payload: message, topic: message}
	];
    return inputs;
}

.....

 node.on('input', function(input, send, done) {
    let inputs = collectInputs(input, node.context());

    prompt(inputs[0].payload, inputs[1].payload);

    send(inputs);
    if (done) {
        done();
    }
}
}

```

However, it seems that only the message title is filed with the input message. I suspect that this is a race condition that I'm not copying with properly.

I do know there is a Join node to consolidate messages into a single input, but I want to avoid introduce a Join for EVERY node with multiple inputs.

What I'm I missing here?

---

<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:** [18 September 2021 08:38 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/2 "2021-09-18T08:38:47Z")

</div>

A basic concept that should be understood is that messages can never arrive at the input of a node at the same time.

You can however join messages.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<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:** [18 September 2021 08:59 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/3 "2021-09-18T08:59:24Z")

</div>

If you don't want to use a Join node you will have to replicate the Join function in your node. Save the previous values for each topic in the node context then you can get the latest value of each.

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [18 September 2021 09:03 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/5 "2021-09-18T09:03:05Z")

</div>

Thanks, Colin. @Colin  
That's something I was trying in my post with the code snippets.  
However, it seems that context is working in an unexpected order.  
Am I using it in the wrong way?

---

<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:** [18 September 2021 09:12 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/6 "2021-09-18T09:12:54Z")

</div>

Ah yes, so you are. I am not sure it is valid to pass the context into a function like that. I would test it initially just using the context inline rather than passing it in, and see if that works.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [18 September 2021 09:18 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/7 "2021-09-18T09:18:14Z")

</div>

@kakyoism I am curious, what type of strange node are you using to feed your messages into? The one that says "Hello World"

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [18 September 2021 09:25 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/8 "2021-09-18T09:25:56Z")

</div>

It's just a node that tries to receive two inputs: title/message, to pop up a message box with the inputs. @Gunner

I've just tried to inline that function and the result is the same. The extracted info from context is out of order @Colin

```auto
inputs: [{"payload":"my name is ","topic":"my name is "},{"payload":"default message","topic":"default message"}]

```

I expect to have

```auto
inputs: [{"payload":"my name is ","topic":"title"},{"payload":"a weird name","topic":"message"}]

```

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [18 September 2021 09:28 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/9 "2021-09-18T09:28:21Z")

</div>

> [@kakyoism](#):
>
> It's just a node that tries to receive two inputs: title/message, to pop up a message box with the inputs.

And what is this node called?

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [18 September 2021 09:28 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/10 "2021-09-18T09:28:55Z")

</div>

it's called `hello_world`. @Gunner

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [18 September 2021 09:33 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/11 "2021-09-18T09:33:25Z")

</div>

I regret even asking 🤪 It sure isn't any of the old and apparently meaningless nodes called "hello world" that I can find in the palette.

**EDIT** - Based on your prior posts, I think I figured out that you are making some form of a contrib-node of your own... thus my inability to find more info about it 😛

---

<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:** [18 September 2021 09:38 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/12 "2021-09-18T09:38:38Z")

</div>

> [@kakyoism](#):
>
> ```auto
> case 'message':
> title = msg.payload;
> 
> ```

Hmm! Something wrong there I think.

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [18 September 2021 09:54 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/13 "2021-09-18T09:54:45Z")

</div>

Argh! You are totally right. @Colin  
I'm sorry to have you guys debug this with me now...

But even with that fixed, I still don't want the "default message" to kick in at all. It seems inevitable with the two branches of Change nodes.

Besides, I just found out a fundamental flaw of my approach:

The node inputs/outputs are all key-value pairs, i.e., named. So I can't guarantee that the names of the other nodes' outputs would match the exact input names of the node of concern.

This is a bit annoying because I really want to replicate other FBP-inspired systems' UX:

- Simply try to connect any output port to any input port
- If they work with each other, they just work.
- Otherwise, the wire simply won't connect, and error logs clearly show the incompatibility.

I know this must be hard because JS generally allows passing anything around. But from the UX point of view I'd love to minimize the adaptors between custom nodes. Maybe I'm asking too much with Node-RED. Are there good design patterns to handle many-to-many in-out ports situations with minimum adaptations?

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [18 September 2021 10:18 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/14 "2021-09-18T10:18:04Z")

</div>

I now start to understand why systems like MaxMSP forces a port evaluation order based on the port visual topology around the node shape, e.g., right-to-left, top-to-bottom.

But I suspect that supporting multiple inputs in the above fashion would end up with a more intuitive user experience. Nodes would then be decoupled without the name-matching issues.

On a side node, [this XKCD post](https://xkcd.com/2044/) seems very relevant IMHO.

---

<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:** [18 September 2021 11:08 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/15 "2021-09-18T11:08:04Z")

</div>

I don't fully understand what you are trying to achieve. Can you explain in words of one syllable (or occasionally 2)?

---

<div class="post-metadata">

**Author:** ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)\
**Post date:** [18 September 2021 11:29 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/16 "2021-09-18T11:29:40Z")

</div>

@Colin Well, I wanted to directly connect any number of outputs to any number of inputs without having to worry about anything in-between, like this

 ![jqBma8B7237bVxgb4oGLwno0yxUQwESESyF2tXhFmDk](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e69a5b21399ac89dd4953a4d8727f64673f56e2d.png)

But I've got the answer from [another post on this forum](https://discourse.nodered.org/t/why-dont-node-red-nodes-allow-multiple-inputs/10973).

---

<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:** [17 November 2021 11:29 UTC](https://discourse.nodered.org/t/how-to-synchronize-two-input-messages/51190/17 "2021-11-17T11:29:58Z")

</div>

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