Web3.js: Troubleshooting Issues with Websocket Connection

web3jswebsocket

I use latest Web3 version (1.6.1) code in Node.js as provided in docs. But when I run:

var Web3 = require('web3');
var web3 = new Web3( new Web3.providers.WebsocketProvider('wss://dex.binance.org/api/ws'));
web3.onopen = function (evt) {
    console.log('WS opened');
}

it never connects. When I try plain WS:

var WebSocket= require('ws');
var WS = new WebSocket('wss://dex.binance.org/api/ws');
WS.onopen = function (evt) {
    console.log('WS opened');
}

It works. What is the issue and how to make Web3 to work with WebSockets.


(note, I've tried this but neither those codes work, and I dont want to use 2years old version package which was "working")

Best Answer

All available end-point rpc url are here https://docs.binance.org/smart-chain/developer/rpc.html. HttpProvider works well. For Websocket run your own BSC Full node or Light Node.

Related Topic