How to make node function run as fifo

I have a node function and I want to run as fifo,
for example, if I received "A" message then "B" message
I want the node executes "A" message firstly then "B" message secondly

Hi @mohamedEmadEldien

that is exactly how nodes work - they operate on the messages in the order they are received.

Do you mean you want a Function node to wait until it has finished processing message A before it even starts to process message B?

Thanks @knolleary,
Yes I want a Function node to wait until it has finished processing message A before it even starts to process message B

Is it not the way it already works? Unless you start a timer on message A and want the timer to kick-in, doing it's stuff and only then when finished, start processing message B

What are you doing in the function?

It sends a direct message to Twitter using twit module
in some cases, I send message1 then message2 but Twitter receive message2 and display it before message1
so I need to send message1 and receive the response then send message2

Have you used a debug node to check they are definitely going into the twitter node in the right order? It seems unlikely that they would overtake each other.

However, if you do need to do this then one way is to use node-red-contrib-semaphore, which I have used for tasks like this.

1 Like

You could declare a variable that you set to true and save to node context if you start processing input A. If this is processing var is true and another message (B) arrives you push it to a queue which you also save to context. Than every time you finish processing something at the end of the function you check if the queue you get from context is empty and if not continue to process B now.
So something like:

let queue = context.get("queue") || []; //get queue or empty
let processingNow = context.get("processingNow") || false; //state
//where you do your work:
function processing(input){
    //do your processing example setTimeout, this is where your actual work would go and not setTimeout
    setTimeout(()=>{
        //when finished send result 
        node.send({payload:input});
        let checkQueue = context.get("queue"); //check the queue and get next item if not empty
        if (checkQueue.length !== 0) {
            const next = checkQueue.shift();
            processing(next); //function calls itself with next item
        } else {
            context.set("processingNow", false); //otherwise set the processing var back to false when done
        }
    }, 3000);
}
if (processingNow) {
    //if processing and queue not empty
    queue.push(msg.payload);
    context.set("queue", queue);
    return null;
} else {
    //call your processing and set to processing
    context.set("processingNow", true);
    processing(msg.payload);
}
return null;

Thats of course very rough and just an example

1 Like

I think he is using https://www.npmjs.com/package/twit directly in the function node.

1 Like

In addition to his suggestion of node-red-contrib-semaphore, @Colin has made clever use of the node-red-contrib-queue-gate node to do essentially what you want. See the third example flow distributed with the node and described in the README, Retain Until Processed.

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