I would like to understand and form a consistent variable that can be retrieved on page updates and across different browsers. I currently use the code below, but I'm not sure how to retrieve the states. Anyone who can help I would appreciate it.
My solution to a similar problem was to ensure that every message sent to the template, or sent from the template, includes all the state information (in a property, msg.state for example). Then when the dashboard is opened in a new browser, or the page is refreshed, the last message sent to or sent from the template will be sent to it so it knows the current state.
I recently updated the Events Architecture documentation to assist with understanding in this space, in particular, the "Dashboard Actions" Events Flow.
What it shows is the difference between the client actions, when you run send() in a ui-template, that fires the widget-send event, which does store any payload/msg you send into the server-side store for that widget.
In turn, what that means is, given our "Loading Event Flow", we can see that this retrieves the latest msg stored against a widget/ui-template:
The widget itself will automatically populate the client-side msg object when the ui-template first loads, with whatever data is stored in the server-side data store (i.e. the last thing sent via send()), and so your watch on msg is a feasible approach here as that will catch on first load. The disadvantage of this approach is that it will run that for all incoming messages too, which also modify msg.
The other option, which I haven't actually exposed too much in the docs because the state is auto-loaded into msg, but does work (I've just tested, and I'll update docs accordingly):
this.$socket.on('widget-load:'+this.id, (msg) => {
// do stuff with the loaded msg
})
Which will only run when the widget is first loaded, and if there is a msg to load. It does not fire when there is no data stored in the server-side store for a widget.
FYI @hotNipi as I know this will be useful for you too, potentially even @BartButenaers too
@Colin
Similar concept I have used for years in my html based hack; when the browser is opened and initiates the web socket connection, a status request is sent to Node-RED that responds with all known statuses in a number of messages. For dashboard v1 I use "theScope" instead
The response messages to the request are received by all connected browser clients so they all get synchronized as well (regardless if it is needed or not)
I'm applying this concept to all my material, but I still have a challenge. There are 2 panels open, when the button is toggled in one, this change does not appear instantly in the other. Whenever I toggle one button, I need to refresh the page on the other to get the new status.
This is actually a bug in the underlying Dashboard 2.0 architecture at the moment that @Colin had recently flagged to me. I need to re-visit the events architecture and workout a solution here.
I don't think that applies when using the ui-template does it? That was about core nodes, though I can't find an open issue about it.
@diegodamaceno the solution is to send the new state information back and into the template again so the other instances get updated. Make sure you don't end up in an infinite loop obviously.