# Problem with custom UI node and replaying data

**URL:** https://discourse.nodered.org/t/problem-with-custom-ui-node-and-replaying-data/49478
**Category:** Dashboard
**Tags:** node-red-dashboard
**Created:** [8 August 2021 10:31 UTC](https://discourse.nodered.org/t/problem-with-custom-ui-node-and-replaying-data/49478 "2021-08-08T10:31:52Z")
**Posts on this page:** 1
**Showing post:** 13

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [9 August 2021 23:04 UTC](https://discourse.nodered.org/t/problem-with-custom-ui-node-and-replaying-data/49478/13 "2021-08-09T23:04:04Z")

</div>

After a while of trying to emit a message directly (failing to get access to `emitSocket()` I found out that is is possible to send a message to myself via `node.receive()`

For anybody interested or anybody saying I'm doing it all wrong here are some code fragments:

1. the $watch code:

```auto
                        $scope.$watch('msg', function(msg) {
                            if (!msg) { return; } // Ignore undefined msg
                            if ($scope._data[0]===undefined) {
                                console.log('uPlot first Message > asking for replay');
                                $scope.send({payload:"R"});
                                return;
                            }
                            if (msg.hasOwnProperty('fullDataset')) {
                                console.log('uPlot fullDataset received:',msg);
                                
                                // do something with the full dataset 

                                });
                            } else if (isNewDataPoint(msg)){
                                console.log('uPlot dataPoint received:',msg);

                                // do something with the datapoint
 
                            }
                        });

```

1. in `beforeSend` trigger let's trigger a message to myself

```auto
                    beforeSend: function (msg, orig) {
                        if (orig.msg.payload === 'R') {
                            node.receive(orig.msg);
                            return;
                        }
                        if (orig) {
                            var newMsg = {};

                            // do something here if your node emits data
                        }
                    },

```

1. in `convert` return the full dataset if a replay is requested

```auto
                    convert: function(value,fullDataset,msg,step) {
                        if (msg.payload==='R') {
                            return fullDataset;
                        }
                        var conversion = {
                            updatedValues: {
                                msg:{
                                    fullDataset : []
                                }},
                            newPoint: {},
                        }

                        // do your converting business

                        return conversion;
                    },

```

1. in `beforeEmit` return the full dataset again (if present and well formed)

```auto
                    beforeEmit: function(msg, fullDataset) {
                        if (fullDataset && fullDataset.hasOwnProperty('msg') && fullDataset.msg.hasOwnProperty('fullDataset')) {
                            return fullDataset 
                        };
                        
                        // prepare your payload ready for launch
                        
                        return { msg: newMsg };
                    },

```

and then you should get something like this if you switch the tab or refresh your page:

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

later messages simply add a new datapoint:

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

Let's see if this solution works in a longer development and debugging period

and all of this will later perhaps look like this (just a proof of concept and my own capabilities)

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

---

_[View the full topic](https://discourse.nodered.org/t/problem-with-custom-ui-node-and-replaying-data/49478)._
