Custom socket node

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

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

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

I know but i 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?

Sounds like the same discussion that has been started here: Websocket listen custom node

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

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. :sweat_smile:

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

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

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

No need to make a contrib node.

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

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

Which is it?

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.

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

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

 function wsListenerNode(config) {
        this.path = config.path;
        var node = this;
        node.on('input', function(msg) {  
            //let url =  node.path;
            let url =" ws://"

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

This is my js code for now.

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

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.

1 Like

Its fun to do it the way im doing.

      //let ws = new WebSocket("wss://");
      let path = "ws://";
      let ws = new WebSocket(path);
      ws.onopen = (e) => {
        alert("SOcket Connected");
        setTimeout(function () {
            "SUBSCRIBE some_path... "
        }, 1000);
      ws.onmessage = (e) => {
        console.log("Message sent");
        let file =;
        var stripped = file.trim();
        var values = stripped.split("\n");

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

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?

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

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.

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 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) 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.