Need help rate limiting a flow

I have message A and message B

[{"id":"c43227adbb509818","type":"inject","z":"9d6232bd.2dfe7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A","payloadType":"str","x":230,"y":700,"wires":[["6df3d2f427122156"]]},{"id":"3066e820da6819c0","type":"inject","z":"9d6232bd.2dfe7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"B","payloadType":"str","x":230,"y":740,"wires":[["6df3d2f427122156"]]},{"id":"6df3d2f427122156","type":"trigger","z":"9d6232bd.2dfe7","name":"","op1":"","op2":"","op1type":"pay","op2type":"payl","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":380,"y":720,"wires":[["e2d17a022de5299b"]]},{"id":"e2d17a022de5299b","type":"rbe","z":"9d6232bd.2dfe7","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":510,"y":720,"wires":[["290eb6f2152c6087"]]},{"id":"290eb6f2152c6087","type":"debug","z":"9d6232bd.2dfe7","name":"debug 60","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":720,"wires":[]}]

This is the behaviour I want to achieve.

Every time the first message is sent, meaning by first that there wasn’t any message for 5 seconds, send it immediately and don’t send nothing else for 5 seconds. After those 5 seconds only sent a message if first message is different from last message.

This should work with chained messages. The example I put above does work for the first two messages, but when a third message comes up it won’t wait the 5 seconds, it’ll do:

A > 5 secs > B > before 5 seconds have passed > A

I need it to behave like this

A > 5 secs > B > 5 secs > A > 5 secs > B > 5 secs > A etc… only if during those 5 seconds the last received message is different from the message sent before the start of the last 5 seconds break.

I hope I made myself clear :).

I would probably make sure that the two trigger nodes send a timestamp as part of the msg.

Then I would have function node that does the following.
- get the old timestamp or sets it to 0 if it does not exist.
- compare the old timestamp with the msg timestamp
- if its difference is more than 5 second then do the following.
-- get the old message with flow.get or set it to "" if it does not exist
-- if the old message is different than the msg then to the following.
--- store the new timestamp with flow.set
--- store the message with flow.set.
--- return msg

Thanks for your answer mate.

I ended up using node-red-ratelimit. It does everything I need natively.