# Update node-red datastore from client without sending a message

**URL:** <https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803>\
**Category:** Dashboard\
**Tags:** dashboard-2\
**Created:** [19 June 2024 15:59 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803 "2024-06-19T15:59:56Z")\
**Posts on this page:** 14\
**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:** [19 June 2024 15:59 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/1 "2024-06-19T15:59:56Z")

</div>

I can see from the docs [Building Third Party Widgets](https://dashboard.flowfuse.com/contributing/widgets/third-party.html#communicating-with-node-red) how I can send a message without updating the data store and how to send a message and update the data store at the same time. How can I modify the store without sending a message? Ideally I don't want to update the entire store, just change one property therein.  
Alternatively can I send an event with some data back to the server and handle the data store update myself?

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [19 June 2024 16:23 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/2 "2024-06-19T16:23:52Z")

</div>

Curious to know the use case here... all communication between client and NR is done via Sockets and utilising the `msg` structure

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [19 June 2024 16:24 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/3 "2024-06-19T16:24:56Z")

</div>

You could introduce a [custom event](https://dashboard.flowfuse.com/contributing/widgets/third-party.html#custom-socketio-events), and then have your server-side handler do whatever it needs to do.

---

<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:** [19 June 2024 18:00 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/4 "2024-06-19T18:00:48Z")

</div>

> [@joepavitt](#):
>
> Curious to know the use case here... all communication between client and NR is done via Sockets and utilising the `msg` structure

I have adopted a slightly different data flow. In my server js file, as each message comes in I don't save the incoming message, as received, in the store, instead I build up the complete state in the data store as a set of properties. Then I just send any modified properties to the widget.

When a widget connects it gets sent the complete data store which allows it to initialise itself. Thereafter it expects just modified properties in the incoming messages which it uses to update its copy of the data.  
So in the initialisation section of the js file I have

```auto
  // initialise data store
  base.stores.data.save(base, node, {/* default values if any */})

```

And in `onInput`, showing the code for just one property

```auto
  // pick up existing stored data
  let storedData = base.stores.data.get(node.id)
  ...
  // pick up msg.formattedValue if present and is a string
  if (typeof msg.formattedValue === "string") {
          storedData.formattedValue = msg.formattedValue
  }
  ...
  // store the latest values in our Node-RED datastore
  base.stores.data.save(base, node, storedData)
  // send the message containing just modified properties to attached clients
  send(msg)

```

In the vue file I have exactly the same code processing the message in `this.$socket.on('widget-load:'...)` and in `this.$socket.on('msg-input:'...`. This looks for the expected properties in the message and updates the client side copy accordingly. Then the vue bindings update the DOM automatically. As you can see it is extremely easy and works perfectly, so far at least. Hopefully I have not misunderstood something vital in all this.

The final part of the puzzle is how to handle it if a property changes state in the client, a user clicks a switch for example. I need to update that property in the server data store, but if I use the method of sending a message then the message would have to contain the complete data structure which would be horribly wasteful. So I just need a way of telling the server to update a property in the data store to a new value.

---

<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:** [21 June 2024 07:05 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/5 "2024-06-21T07:05:45Z")

</div>

Do you see any problems with that approach @joepavitt?

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [21 June 2024 07:57 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/6 "2024-06-21T07:57:26Z")

</div>

Haven't had a chance to do the reading of this justice, will hopefully get to it today

---

<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:** [21 June 2024 08:40 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/7 "2024-06-21T08:40:37Z")

</div>

Ok, sorry, I didn't mean to hassle you.

---

<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:** [21 June 2024 09:25 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/8 "2024-06-21T09:25:15Z")

</div>

Can D2 cached data be retained across Node-RED restarts?

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [21 June 2024 14:37 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/9 "2024-06-21T14:37:42Z")

</div>

It cannot - we bind data against the nodes themselves. Although, I opened [New Nodes: In/Out User-Specific Context Store · Issue #1009 · FlowFuse/node-red-dashboard · GitHub](https://github.com/FlowFuse/node-red-dashboard/issues/1009) today, which if setup with persistent storage, then yes, would persist beyond a restart

---

<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:** [21 June 2024 15:42 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/10 "2024-06-21T15:42:49Z")

</div>

Presumably a third party node would be able to access the node red context in it's server side .js file, if it had a particular requirement that needed it.

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [21 June 2024 15:54 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/11 "2024-06-21T15:54:11Z")

</div>

I believe so. @Steve-Mcl I can't see any docs on it, but I suspect this is possible?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 June 2024 16:49 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/12 "2024-06-21T16:49:12Z")

</div>

Yes, it is possible though you should be very clear about what and why in your docs/help info.

For example, I would not be happy if a node was using was accessing (and potentially overwriting) my flow or global context data.

For a quick how to see the additions commit on `cron-plus`: [add context as persistent store target · Steve-Mcl/node-red-contrib-cron-plus@77cf52c · GitHub](https://github.com/Steve-Mcl/node-red-contrib-cron-plus/commit/77cf52c9b09e7dc7e9cf01838e69a1fe40bf4664)

It uses own node context only and presents the user with a choice of available context stores

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d1f1b29565bf4a1dd5746d0b2c80c20895a1e9a.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/300964e665e83005d9884e3b21eec88151d971dc.png)

---

<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:** [21 June 2024 20:27 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/13 "2024-06-21T20:27:24Z")

</div>

> [@Steve-Mcl](#):
>
> It uses own node context only and presents the user with a choice of available context stores

Some of the UIBUILDER nodes do this as well, notably the uibuilder cache node.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7bf993dbee372cd5231123c2e6de3cd2140ea1d6.png)

It _can_ be useful to use a store that other things can also manage - but as you say, there are plenty of risks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 July 2024 20:27 UTC](https://discourse.nodered.org/t/update-node-red-datastore-from-client-without-sending-a-message/88803/14 "2024-07-21T20:27:49Z")

</div>

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