I have created the node-red custom node which will take the URLs to be connected and it connects to multiple endpoints dynamically.
Below is the code that I am usingpackage.json (434 Bytes) package-lock.json (669 Bytes) .
HTML:
<script type="text/javascript">
RED.nodes.registerType('websocketlistener',{
category: 'function',
color: '#de4e61',
defaults: {
name: {value:""}
},
inputs:1,
outputs:1,
icon: "white-globe.svg",
label: function() {
return this.name||"websocketlistener";
}
});
</script>
<script type="text/html" data-template-name="websocketlistener">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/html" data-help-name="websocketlistener">
<p>Based on the input JSON urls it will listen from websocket multiple endpoint</p>
</script>
JAVASCRIPT:
module.exports = function(RED) {
var WSCLIENT = require('ws-reconnect');
function CustomWebSocketListener(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
let urls =msg.payload.urls;
let connections = {};
for(let i=0;i<urls.length;i++){
if(connections[urls[i]]){
}
else{
const wsclient = new WSCLIENT(urls[i],{
retryCount:1, // default is 2
reconnectInterval: 20 // default is 5
});
wsclient.start();
wsclient.on("message",function(data){
node.send(msg);
});
wsclient.on("reconnect",function(){
console.log("websocketlistener :::reconnecting");
});
wsclient.on("connect",function(){
console.log("websocketlistener ::: connected"+urls[i]);
wsclient.send(urls[i]);
});
wsclient.on("destroyed",function(){
console.log("websocketlistener ::: destroyed");
});
connections[urls[i]]=wsclient;
}
}
});
}
RED.nodes.registerType("websocketlistener",CustomWebSocketListener);
}
Below is the command I am using for linking node to node-red.
In custom node folder: npm link node-red-contrib-websocketlistener
Then in node-red I ran the following command: npm link "E:\node-red custom node\node-red-contrib-websocketlistener"
But after the above steps also it is not adding in node-red.