# Queueing input messages

**URL:** <https://discourse.nodered.org/t/queueing-input-messages/29485>\
**Category:** Developing Nodes\
**Created:** [4 July 2020 08:41 UTC](https://discourse.nodered.org/t/queueing-input-messages/29485 "2020-07-04T08:41:40Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [4 July 2020 08:41 UTC](https://discourse.nodered.org/t/queueing-input-messages/29485/1 "2020-07-04T08:41:40Z")

</div>

In a node I am creating I want to service messages sequentially rather than in parallel (it runs async tasks). Is there a way I can tell node to hold the messages for me (so `on('input')` will not get called till I re-enable it) or do I need to queue them internally?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 July 2020 08:48 UTC](https://discourse.nodered.org/t/queueing-input-messages/29485/2 "2020-07-04T08:48:30Z")

</div>

You will need to queue them internally.

If your node's message handling is entirely synchronous then it's not a problem.

But it is an interesting idea for a node to be able to tell the runtime it can only be invoked one message at a time - relying on the node.done function to trigger the release of the next message. Something to consider for the future.

---

<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:** [4 July 2020 08:56 UTC](https://discourse.nodered.org/t/queueing-input-messages/29485/3 "2020-07-04T08:56:51Z")

</div>

> [@knolleary](#):
>
> You will need to queue them internally.

OK, thanks. I imagine this is not an uncommon requirement.  
This is actually a follow on from my previous thread about cleaning up where there are mulitple processes to kill caused by handling messages in parallel. I realised that though the node functions perfectly well in this mode, since there are progress messages passed on from spawned tasks those would be all mixed up and it would be a pain for the user to split them up. There isn't actually any significant benefit to running them in parallel so I have decided to serialise them instead.

---

<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:** [4 July 2020 11:02 UTC](https://discourse.nodered.org/t/queueing-input-messages/29485/4 "2020-07-04T11:02:12Z")

</div>

In case anyone finds this and wants to know a solution, it was easier than I anticipated.

```auto
        let msgQueue = [];

        node.on('input', function(msg, send, done) {
          // push this one onto the queue
          msgQueue.push( {thisMsg: msg, thisSend: send, thisDone: done} )
          // if there is now only one in the queue then nothing going on so handle it immediately
          // otherwise already dealing with one so nothing more to do at the moment
          if (msgQueue.length == 1) {
            handleMessage(msgQueue[0].thisMsg, msgQueue[0].thisSend, msgQueue[0].thisDone)
            // leave it in the queue, it will be shifted out when done
          }
        });

```

Then in `handleMessage()` after calling `done()`

```auto
              // shift this message off the front of the queue
              msgQueue.shift()
              // see if any more to do
              if (msgQueue.length > 0) {
                // yes, start the next one
                handleMessage(msgQueue[0].thisMsg, msgQueue[0].thisSend, msgQueue[0].thisDone)
              }

```

---

<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:** [2 September 2020 11:02 UTC](https://discourse.nodered.org/t/queueing-input-messages/29485/5 "2020-09-02T11:02:13Z")

</div>

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