Simple repeat last msg node to re-send MQTT

Before I re-invent the wheel, is there a node that stores the last msg that passes thru it and re-sends it if it gets something like msg.repeat = True?

Don't mind coding one up but sometimes you find out someone else has already done it - googling didn't find anything for me


If msg.repeat is true what should happen with the current message arriving with msg.repeat?

Well, I've been wanting to create such a beast to go with my uibuilder node. However, I do have a simple work-around using a function node if you don't mind fitting in with my standards for cache-replay and cache-clear.

Of course, if you'd like to create a cache node that works with uibuilder, that would also be great! :smile:

The standard message types I've defined in order to work with uibuilder and caching:

And the repo for the - as yet non-existent cache node:

1 Like

If not true, it should pass it thru for minimal side-effects

Post the code and I'll see if its simple enough for me to use :slight_smile:

Good evening Mister Walters,

Welcome to the Node-RED forum ! :wink:

The pass-through sounds like an interesting new feature to add to the node-red-contrib-msg-resend node. For a few beers I can introduce you to the author of that node... Perhaps we can convince him to add this new functionality.

I'm don't need anything as complex as this but thanks

See here for the link to the code:

it was the second link in my last post - don't be fooled by Discourse insisting on only putting the text for the repo description in the links.

Well - I think that lot is way above my programming pay-grade :slight_smile:

Seriously, the function workaround only needs copying from the WIKI post and you have 2 simple msgs, one to replay the cache and one to clear it.

You know the phrase "can't see the wood for the trees" :slight_smile:
That's how the wiki post appears to me

Could I as k you to copy /paste the code since you know how to find it? :slight_smile:

Sorry, my fault - I reposted the wrong link! Try this one:

In fact, I'll save you the visit ...

If you want to cache and replay messages without another node, here is a very simple example flow.


This is the function that is key. As always, it is up to you to keep track of how much memory is being consumed. The replayed messages will go to the client that requested them if it can identify it.

The function only keeps the last 1 message of each topic and this version has no method to clear the cache.

// Expects input msgs with topic set

// saved context
var homeMsgs = context.get('homeMsgs') || {}

// Replay cache if requested
if ( msg.hasOwnProperty('cacheControl') && msg.cacheControl === 'REPLAY' ) {
    for (var topic in homeMsgs) {
        // Only send to a single client if we can
        if ( msg.hasOwnProperty('_socketId') )
                "topic": topic, 
                "payload": homeMsgs[topic],
                "_socketId": msg._socketId
                "topic": topic, 
                "payload": homeMsgs[topic]
    return null

// ignore cacheControl and uibuilder control messages
if ( msg.hasOwnProperty('cacheControl') || msg.hasOwnProperty('uibuilderCtrl') ) return null

// Keep the last msg.payload by topic
homeMsgs[msg.topic] = msg.payload

// save context for next time
context.set('homeMsgs', homeMsgs)

return msg;

Example Flow

Note that the flow doesn't contain the MQTT-in node that drives it. Adapt to your own requirements.

[{"id":"401897b8.9931e8","type":"uibuilder","z":"9974253c.de8db8","name":"","topic":"","url":"home","fwdInMessages":false,"allowScripts":true,"allowStyles":true,"debugFE":false,"copyIndex":true,"x":490,"y":700,"wires":[["d0dec491.704248"],["bbc35ca3.97343","ff280acc.207098"]]},{"id":"d0dec491.704248","type":"debug","z":"9974253c.de8db8","name":"home data out","active":true,"console":"false","complete":"true","x":680,"y":680,"wires":[]},{"id":"889a6459.17d2f8","type":"function","z":"9974253c.de8db8","name":"","func":"// Expects input msgs with topic set\n\n// saved context\nvar homeMsgs = context.get('homeMsgs') || {}\n\n// Replay cache if requested\nif ( msg.hasOwnProperty('cacheControl') && msg.cacheControl === 'REPLAY' ) {\n    for (var topic in homeMsgs) {\n        // Only send to a single client if we can\n        if ( msg.hasOwnProperty('_socketId') )\n            node.send({\n                \"topic\": topic, \n                \"payload\": homeMsgs[topic],\n                \"_socketId\": msg._socketId\n            })\n        else\n            node.send({\n                \"topic\": topic, \n                \"payload\": homeMsgs[topic]\n            })\n    }\n    return null\n}\n\n// ignore cacheControl and uibuilder control messages\nif ( msg.hasOwnProperty('cacheControl') || msg.hasOwnProperty('uibuilderCtrl') ) return null\n\n// Keep the last msg.payload by topic\nhomeMsgs[msg.topic] = msg.payload\n\n// save context for next time\ncontext.set('homeMsgs', homeMsgs)\n\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":700,"wires":[["401897b8.9931e8","b4850396.c9aef"]]},{"id":"bbc35ca3.97343","type":"debug","z":"9974253c.de8db8","name":"home controls","active":true,"console":"false","complete":"true","x":680,"y":720,"wires":[]},{"id":"e35dc750.bbfbe8","type":"link in","z":"9974253c.de8db8","name":"home-replay","links":["ff280acc.207098"],"x":115,"y":700,"wires":[["889a6459.17d2f8"]]},{"id":"ff280acc.207098","type":"link out","z":"9974253c.de8db8","name":"home-controls","links":["e35dc750.bbfbe8"],"x":615,"y":760,"wires":[]},{"id":"b4850396.c9aef","type":"debug","z":"9974253c.de8db8","name":"input messages","active":true,"console":"false","complete":"true","x":480,"y":640,"wires":[]}]

And in case you don't want to install uibuilder. Just note that, to resend the cached info, send in a msg to the function that looks like:

    "cacheControl": "replay",
    "topic": "My Topic"

You can use the msg.topic to control which msg gets replayed. The function will store the last msg of every topic. Obviously, you should take care with memory sizes if you have a lot of topics with lots of data.

1 Like

Can I ask why you need to do that?

I'm sending colour value via MQTT to some remote Neopixels attached to a WEMOS D1 Mini.

I added in sending brightness info on another topic and realised that I needed to re-send the colour info in order to update the Neopixels (due to way I coded things up on the WEMOS)

Also, I'd been thinking about having a repeat node anyway for everyday testing/debugging purposes

I'm just wanting to repeat the payload regardless of topic so I've simplified it down to this

which gets generated to this

if ((msg['payload']) == 'RePeAt') {
  if ((context.get('repeat')) != null) {
    msg['payload'] = (context.get('repeat'));
} else {
  context.set('repeat', (msg['payload']));

Great, well we got there so that's the main thing.

1 Like

Hopefully not to take this OT ..
Why is (msg['payload']) worded in that way? Why not simply (msg.payload)?

It IS off-topic :wink:

But that is an artifact of using Blockly since Blockly might have to deal with complex property names.

1 Like


I was looking for a repeat node and got directed to this thread to find out I'd already done it :slight_smile:


I'll jump in here. (and probably make a fool of myself, but here goes)

The topic includes MQTT.

If you tick the retain box, MQTT will resend the message if anything goes wrong with the delivery.

Or did I miss something else?