# Simple repeat last msg node to re-send MQTT

**URL:** https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558
**Category:** General
**Created:** [4 November 2018 16:18 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558 "2018-11-04T16:18:56Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [4 November 2018 16:18 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/1 "2018-11-04T16:18:56Z")

</div>

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

Simon

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [4 November 2018 16:46 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/2 "2018-11-04T16:46:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 November 2018 16:58 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/3 "2018-11-04T16:58:36Z")

</div>

> [@cymplecy](#):
>
> 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

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! 😄

> **[Message Caching](https://github.com/TotallyInformation/node-red-contrib-uibuilder/wiki/Message-Caching)**
>
> Easily create data-driven web UI's for Node-RED using any (or no) front-end framework. - TotallyInformation/node-red-contrib-uibuilder

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

> **[Control Message Structure](https://github.com/TotallyInformation/node-red-contrib-uibuilder/wiki/Control-Message-Structure)**
>
> Easily create data-driven web UI's for Node-RED using any (or no) front-end framework. - TotallyInformation/node-red-contrib-uibuilder

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

> **[GitHub - TotallyInformation/node-red-contrib-infocache: SUPERSEDED - see...](https://github.com/TotallyInformation/node-red-contrib-infocache)**
>
> SUPERSEDED - see README. Cache & replay messages in Node-RED flows. Optionally by topic and client/user id. - GitHub - TotallyInformation/node-red-contrib-infocache: SUPERSEDED - see README. Ca...

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [4 November 2018 17:28 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/4 "2018-11-04T17:28:53Z")

</div>

> [@ghayne](#):
>
> If msg.repeat is true what should happen with the current message arriving with msg.repeat?

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

> [@TotallyInformation](#):
>
> 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.

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

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [4 November 2018 18:23 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/5 "2018-11-04T18:23:03Z")

</div>

Good evening Mister Walters,

Welcome to the Node-RED forum ! 😉

The **pass-through** sounds like an interesting new feature to add to the [node-red-contrib-msg-resend](https://github.com/bartbutenaers/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.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [4 November 2018 18:58 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/6 "2018-11-04T18:58:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 November 2018 21:25 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/7 "2018-11-04T21:25:39Z")

</div>

> [@cymplecy](#):
>
> Post the code and I'll see if its simple enough for me to use

See here for the link to the code:

> **[Control Message Structure](https://github.com/TotallyInformation/node-red-contrib-uibuilder/wiki/Control-Message-Structure)**
>
> Easily create data-driven web UI's for Node-RED using any (or no) front-end framework. - TotallyInformation/node-red-contrib-uibuilder

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.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [4 November 2018 21:30 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/8 "2018-11-04T21:30:48Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 November 2018 21:47 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/9 "2018-11-04T21:47:01Z")

</div>

> [@cymplecy](#):
>
> I think that lot is way above my programming pay-grade

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.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [4 November 2018 21:48 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/10 "2018-11-04T21:48:46Z")

</div>

You know the phrase "can't see the wood for the trees" 🙂  
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? 🙂

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 November 2018 21:54 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/11 "2018-11-04T21:54:14Z")

</div>

> [@cymplecy](#):
>
> You know the phrase "can't see the wood for the trees" 🙂  
> 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?

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

> **[Cache and Replay Messages without using node red contrib infocache](https://github.com/TotallyInformation/node-red-contrib-uibuilder/wiki/Cache-and-Replay-Messages-without-using-node-red-contrib-infocache)**
>
> Easily create data-driven web UI's for Node-RED. Single- & Multi-page. Multiple UI's. Work with existing web development workflows or mix and match with no-code/low-code features. - Tot...

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.

## Function

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.

```javascript
// 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') )
            node.send({
                "topic": topic, 
                "payload": homeMsgs[topic],
                "_socketId": msg._socketId
            })
        else
            node.send({
                "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.

```auto
[{"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:

```auto
{
    "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.

---

<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: [5 November 2018 08:30 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/12 "2018-11-05T08:30:29Z")

</div>

> [@cymplecy](#):
>
> 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?

Can I ask why you need to do that?

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [5 November 2018 09:11 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/13 "2018-11-05T09:11:07Z")

</div>

> [@Colin](#):
>
> 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

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [5 November 2018 13:33 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/14 "2018-11-05T13:33:18Z")

</div>

I'm just wanting to repeat the payload regardless of topic so I've simplified it down to this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/384a87b7941c431defad331596d8453a31aef5b7.png)

which gets generated to this

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [5 November 2018 20:47 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/15 "2018-11-05T20:47:12Z")

</div>

> [@cymplecy](#):
>
> so I've simplified it down to this

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

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [5 November 2018 21:10 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/16 "2018-11-05T21:10:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [5 November 2018 21:15 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/17 "2018-11-05T21:15:03Z")

</div>

> [@Paul-Reed](#):
>
> Hopefully not to take this OT ..  
> Why is `(msg['payload'])` worded in that way? Why not simply `(msg.payload)` ?

It IS off-topic 😉

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

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [5 November 2018 21:42 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/18 "2018-11-05T21:42:20Z")

</div>

> [@TotallyInformation](#):
>
> since Blockly might have to deal with complex property names.

Yep  
...............

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [14 August 2019 19:48 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/19 "2019-08-14T19:48:44Z")

</div>

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

#LOL

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [14 August 2019 21:37 UTC](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558/20 "2019-08-14T21:37:14Z")

</div>

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?

[Next page](https://discourse.nodered.org/t/simple-repeat-last-msg-node-to-re-send-mqtt/4558.md?page=2)
