Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to RSK public nodes over websockets?

Tags:

websocket

rpc

rsk

I am trying to connect to RSK Mainnet or RSK Testnet over websockets. Here's what I tried for Mainnet:

const wsProvider = new Web3.providers.WebsocketProvider('ws://public-node.rsk.co');
const web3 = new Web3(wsProvider);
web3.eth.subscribe('newBlockHeaders', function(error, blockHeader){
    if (!error) {
        console.log("new blockheader " + blockHeader.number)
    } else {
        console.error(error);
    }
});

with this result:

connection not open on send()
Error: connection not open

And I did the same with Testnet but using ws://public-node.testnet.rsk.co, getting similar outcome. Neither of these work, as seen in the errors above. How can I connect?

like image 976
Milton Avatar asked Mar 04 '21 13:03

Milton


Video Answer


2 Answers

Milton I am not sure, but I think websocket is not enabled in public nodes.

Usually it is not enabled in others public blockchain nodes that I know.

like image 90
Solange Gueiros Avatar answered Sep 28 '22 11:09

Solange Gueiros


RSK public nodes expose JSON-RPC endpoints only over HTTP.

They do not expose JSON-RPC endpoints over websockets, so unfortunately, you are not able to do exactly what you have described.

However, you can achieve something equivalent by running your own RSK node, and use this to establish websockets connections.

Here are the RSK configuration options for RPC .

Also, you can see the default configuration values in the "base" configuration file, for rpc.providers.ws

  ws {
      enabled = false
      bind_address = localhost
      port = 4445
  }

Additionally, you should include the /websocket suffix in your endpoint. Default websocket endpoint when running your own node is: ws://localhost:4445/websocket. Therefore, update the initial part of your code, such that it looks like this:

const wsProvider = new Web3.providers.WebsocketProvider('ws://localhost:4445/websocket');
const web3 = new Web3(wsProvider);
like image 20
bguiz Avatar answered Sep 28 '22 10:09

bguiz