How to connect BSC wesocket provider with Web3J or Web3js

9.4k Views Asked by At

I`ve tried to connect to the websocket api over getblock.io but it always fails. Here below is JS code. But HttpProvider works fine on "https://bsc-mainnet.web3api.com/v1/API_KEY".

web3ws = new Web3(new Web3.providers.WebsocketProvider('wss://bsc.getblock.io/mainnet/?api_key=API_KEY'));

if (web3ws.isConnected)
            console.log("Connected");
        else 
            console.log("Not Connected"); 

Am I missing something here ? Should i have to pay it first to able to use this service?

2

There are 2 best solutions below

5
On

GetBlock currently (May 2021) doesn't support WebSocket provider.

From their pricing page:

Webhooks & WebSockets

Coming soon! We are constantly adding new features and upgrading the service


If HTTPS provider is sufficient to your use case (e.g. if you don't need to use the web3 subscriptions), you can use any provider that only supports HTTPS connection. Including GetBlock or the recommended providers stated in the BSC docs

Othwerwise, you'll need to use a different third-party provider that supports WebSockets or configure your own.

2
On

Our WebSockets for BSC have been working during all this time. The WebSockets which Petr Hejda meant is a tool used for event notifications. Currently, it's under development, we're working on its implementation.

We're going to check the JS code that you sent to understand, if everything is correct. And you shouldn't pay, if you want to test the service as we provide 40K requests/day for free.