How to use websocket server-server in node.js?

1

Hi, I am new to node and I was wondering how I can consume a websockets service from node, os server-server, the second is an external server here the link: link , Thank you very much community for your answers.

    
asked by David Fernando Zapata 25.02.2018 в 17:33
source

1 answer

0

I suggest using the 'ws' library that is available in npm: npm install ws . Then you can consume the websockets API in this way:

var WebSocket = require('ws');
var ws = new WebSocket("wss://ws.blockchain.info/inv");

ws.on('open', function() {
  ws.send(JSON.stringify({"op":"ping"}));
});

ws.on('message', function(message) {
  console.log('Received: ' + message);
});

ws.on('close', function(code) {
  console.log('Disconnected: ' + code);
});

ws.on('error', function(error) {
  console.log('Error: ' + error.code);
});
    
answered by 26.02.2018 / 03:01
source