# Adding function to node

**URL:** <https://discourse.nodered.org/t/adding-function-to-node/29160>\
**Category:** Developing Nodes\
**Created:** [29 June 2020 10:44 UTC](https://discourse.nodered.org/t/adding-function-to-node/29160 "2020-06-29T10:44:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastin](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Mastin](https://discourse.nodered.org/u/Mastin)\
**Post date:** [29 June 2020 10:44 UTC](https://discourse.nodered.org/t/adding-function-to-node/29160/1 "2020-06-29T10:44:41Z")

</div>

Good morning,  
I am modifying the ocpp node for personal use.  
I have added a function that makes it reconnect if it loses the connection (this only did it if you did a deploy).  
The author of the module that makes the reconnection.

> **[pladaria/reconnecting-websocket](https://github.com/pladaria/reconnecting-websocket)**
>
> Reconnecting WebSocket. For Web, React Native, cli (Node.js) - pladaria/reconnecting-websocket

Indicates that a ws.reconnect () call can be made for the module to reconnect.

What I would like to know is how can I add the option that if it receives by the msg.payload the word "car", execute the ws.reconnect ().

I have tried adding this to the module js.  
if (msg.payload == 'car'){  
ws.reconnect();  
}  
but it does not work...  
Could someone guide me?

Thank you!

---

<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:** [29 June 2020 11:05 UTC](https://discourse.nodered.org/t/adding-function-to-node/29160/2 "2020-06-29T11:05:46Z")

</div>

> [@Mastin](#):
>
> I have tried adding this to the module js.

Hey,  
I think you should add it [here](https://github.com/Argonne-National-Laboratory/node-red-contrib-ocpp/blob/f85c90a26d3ad228031c86e4733620f81b0bb795/ocpp/ocpp-cp-json.js#L162):

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f2832de0914004ece0a874d480dcc0ff9947dac1.png)

Because that is where the input message (msg) arrives ...

---

<div class="post-metadata">

**Author:** ![Mastin](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Mastin](https://discourse.nodered.org/u/Mastin)\
**Post date:** [29 June 2020 11:32 UTC](https://discourse.nodered.org/t/adding-function-to-node/29160/3 "2020-06-29T11:32:18Z")

</div>

It works perfect!

Thank you very much for your help!!!

---

<div class="post-metadata">

**Author:** ![Mastin](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Mastin](https://discourse.nodered.org/u/Mastin)\
**Post date:** [30 June 2020 11:20 UTC](https://discourse.nodered.org/t/adding-function-to-node/29160/4 "2020-06-30T11:20:12Z")

</div>

One question more.

In this part of my code:

```auto
    ws.addEventListener('close', function(){
      logger.log('info', `Closing websocket connectio to ${csUrl}`);
      node.status({fill: 'red', shape: 'dot', text: 'Cerrado...'});
      node.wsconnected = false;

```

I would like you to take a text out of the payload, for example, 'Dog'.  
I have tried adding  
msg.payload = 'Dog';

But it shouldn't work that way.

Any ideas?

Thank you very much!!

---

<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:** [30 June 2020 13:52 UTC](https://discourse.nodered.org/t/adding-function-to-node/29160/5 "2020-06-30T13:52:08Z")

</div>

> [@Mastin](#):
>
> of my code

Based on only one function of your code it is not easy to see what you are doing.  
Is that the same function as [this](https://github.com/Argonne-National-Laboratory/node-red-contrib-ocpp/blob/f85c90a26d3ad228031c86e4733620f81b0bb795/ocpp/ocpp-cp-json.js#L162) original function?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65c273d41d5f7ccf416a63ffc47b5a00b7e91196.png)

I don't think you can access there your input message, at least like the code is structured like it is now.

> [@Mastin](#):
>
> But it shouldn't work that way.

Not sure what you mean. How do you want it to work then?  
Please show a complete code snippet with the msg handling code included. And explain what you expect, and which result you currently get instead...

---

<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:** [29 August 2020 13:52 UTC](https://discourse.nodered.org/t/adding-function-to-node/29160/6 "2020-08-29T13:52:08Z")

</div>

Closed on 29 August 2020 13:52 UTC

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