0

I tried to use WebSocket to listen to events from tendermint as per the code below. It didn't work. I like to know if anyone has a snippet of how to connect via WebSocket on NodeJS?

const WebSocket = require('ws');
const ws = new WebSocket('ws://192.168.1.45:26657');
ws.on('open', function open() {
  ws.send('something');
});
ws.on('message', function incoming(data) {
  console.log(data);
});

ws.onerror = error => {
  console.log(`WebSocket error: ${error}`)
}

error

message:'Unexpected server response: 200'
Progman
  • 16,827
  • 6
  • 33
  • 48
Pakorn K
  • 111
  • 1
  • 11

1 Answers1

0

It worked. I forgot to send the command subscribe.

    'ws.on('open', function open() {
           ws.send(JSON.stringify({ "jsonrpc": "2.0", "method": "subscribe", "params": 
          ["tm.event='NewBlock'"], "id": 1 }));
          });
Pakorn K
  • 111
  • 1
  • 11