# Custom socket node

**URL:** <https://discourse.nodered.org/t/custom-socket-node/43553>\
**Category:** Developing Nodes\
**Created:** [2 April 2021 13:19 UTC](https://discourse.nodered.org/t/custom-socket-node/43553 "2021-04-02T13:19:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [2 April 2021 13:19 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/1 "2021-04-02T13:19:36Z")

</div>

How do I create code to listen to websocket server. For example: ws://example.com/webEvent

I dont know how to create web socket or even how to listen to it . Any help would be much appriciated

---

<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:** [3 April 2021 21:57 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/2 "2021-04-03T21:57:12Z")

</div>

Have you checked the pallet of nodes? Node-RED already has a websocket node that will do this for you.

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 06:48 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/3 "2021-04-06T06:48:25Z")

</div>

I know but i want to make a custom one with 1 extra functionallity.

---

<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:** [6 April 2021 07:02 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/4 "2021-04-06T07:02:48Z")

</div>

> [@jake123](#):
>
> want to make a custom one with 1 extra functionallity

If you have no idea how to create or listen you are in for a steep learning curve creating a node.

Maybe if you explain this "1 extra functionallity" you could make a feature request for the existing websocket nodes?

Maybe you could even provide a PR to the existing websocket nodes?

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [6 April 2021 07:03 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/5 "2021-04-06T07:03:45Z")

</div>

Sounds like the same discussion that has been started here: [Websocket listen custom node](https://discourse.nodered.org/t/websocket-listen-custom-node/43551)

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 07:07 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/6 "2021-04-06T07:07:15Z")

</div>

```auto
var stripped = msg.payload.trim();
var values = stripped.split('\n');

var datapointname = values[0].substring(37, values[0].length - 14);
data = Number(values[1]);

```

this is the extra functionallity

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [6 April 2021 07:18 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/7 "2021-04-06T07:18:31Z")

</div>

What's wrong with using the function node?

I don't see the need to create yet another custom for such a specific use-case. Other than educational purposes, of course. 😅

Also, this functionality is too specific to be included into the standard websocket nodes.

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 07:18 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/8 "2021-04-06T07:18:45Z")

</div>

I just need code that will create a socket that connects to ws://example.com

---

<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:** [6 April 2021 07:19 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/9 "2021-04-06T07:19:35Z")

</div>

so drop a websocket node on the editor and set the address.

No need to make a contrib node.

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 07:19 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/10 "2021-04-06T07:19:41Z")

</div>

Yeah, educational purpose it is. Im trying to create a flow that will contain less nodes that it will normally contain.For training

---

<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:** [6 April 2021 07:20 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/11 "2021-04-06T07:20:29Z")

</div>

> [@jake123](#):
>
> Im trying to create a flow

So you are creating a flow and NOT a new contrib node?

Which is it?

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 07:21 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/12 "2021-04-06T07:21:41Z")

</div>

Its flow of different custom nodes. I could create whole flow with the nodes already installed in node-red. But i want it to be with less nodes so i need to use new contrib nodes.  
For example i dont use join node - I created a custom node that handles the function node + join node. So instaed of using 2 nodes im using 1. Thats the idea.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [6 April 2021 07:24 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/13 "2021-04-06T07:24:31Z")

</div>

You could also consider using subflows. Much easier than creating custom nodes.

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 07:27 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/14 "2021-04-06T07:27:07Z")

</div>

Yea i know i did that. But i understand all of that so i went on and gave myself a harder task.But the problem is i have never used a websocket node in javascript so i dont know the code to connect to 1.  
I have my websocket server running but i cant connect to it and listen to the data it recives

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 07:28 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/15 "2021-04-06T07:28:44Z")

</div>

```auto
 function wsListenerNode(config) {
        RED.nodes.createNode(this,config); 
        this.path = config.path;
        var node = this;
        node.on('input', function(msg) {  
            //let url = node.path;
            let url =" ws://example.com/webevent/"

            let socket = new ws(url);
            socket.on('message',(e) => {
               
            })
        
            
            node.send(msg)

```

This is my js code for now.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [6 April 2021 07:44 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/16 "2021-04-06T07:44:28Z")

</div>

> [@jake123](#):
>
> But the problem is i have never used a websocket node in javascript so i dont know the code to connect to 1.

A good start is always looking at the existing code and study the corresponding docs of the lib in use:

> **[ws](https://www.npmjs.com/package/ws)**
>
> Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js. Latest version: 8.15.1, last published: 11 days ago. Start using ws in your project by running \`npm i ws\`. There are 16824 other projects in the npm registry...

---

<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:** [6 April 2021 08:08 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/17 "2021-04-06T08:08:26Z")

</div>

The code is already in the existing websocket node - all you need to do is copy that and amend it.

I hope that you aren't publishing this to npm though - as has been said, all of this can already be done easily with the existing nodes. As we already have thousands of nodes, it becomes very confusing for people if there are new ones popping up that are very similar, especially to core nodes.

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [6 April 2021 08:26 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/18 "2021-04-06T08:26:31Z")

</div>

Its fun to do it the way im doing.

```auto
    <script>
      //let ws = new WebSocket("wss://echo.websocket.org");
      let path = "ws://example.com";
      let ws = new WebSocket(path);
      ws.onopen = (e) => {
        console.log(e);
        alert("SOcket Connected");
        setTimeout(function () {
          ws.send(
            "SUBSCRIBE some_path... "
          );
        }, 1000);
      };
      ws.onmessage = (e) => {
        console.log("Message sent");
        let file = e.data;
        console.log(file);
        var stripped = file.trim();
        var values = stripped.split("\n");

        var datapointname = values[0].substring(37, values[0].length - 14);
        data = Number(values[1]);
        console.log({
          name: datapointname,
          value: data,
        });
      };
    </script>

```

I tried this in vanilla js and it works fine. Every 5 sec i get different value.  
The problem now is to implement this code in node-red js file.

I tried implementing this in node-red and i get an error websocket is not defined. Any idea why?

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [6 April 2021 09:11 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/19 "2021-04-06T09:11:52Z")

</div>

Yes, your code is Browser JavaScript. Node-RED runs on NodeJS, totally different environments. 😉

Try to follow my previous suggestion, look at the existing code of the websocket node and make yourself familiar with the `ws` lib.

There's also no need to try this in a custom node for experimenting at first. Use a plain .js file and execute it with Node.

---

<div class="post-metadata">

**Author:** ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)\
**Post date:** [7 April 2021 05:42 UTC](https://discourse.nodered.org/t/custom-socket-node/43553/20 "2021-04-07T05:42:25Z")

</div>

For what it is worth, when it comes to WS in node red, I would encourage you to not re invent the wheel. To get them to stay connected and reconnect reliably over a period of weeks/months is a bit of a challenge . I use the WS for a OCPP server (electric vehicle charging protocol ) and are using the [GitHub - Argonne-National-Laboratory/node-red-contrib-ocpp: Open Charge Point Protocol Node-Red Nodes](https://github.com/Argonne-National-Laboratory/node-red-contrib-ocpp) node.

It works pretty well but to be reliable over weeks I have had to replace the WS library with the reconnecting-websocket library. [GitHub - pladaria/reconnecting-websocket: Reconnecting WebSocket. For Web, React Native, cli (Node.js)](https://github.com/pladaria/reconnecting-websocket) I have an open pull request on the OCPP library to incorporate it. Every time I republish my flows I hold my breath but in general it is VERY stable up now for effectively 6 months.

A very long way of saying I would suggest there are a number of fun optimisation problems I would address before WS.

[Next page](https://discourse.nodered.org/t/custom-socket-node/43553.md?page=2)
