Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k points)

I am on web3 version 1.0.0-beta.27 and I am running a private ethereum blockchain for testing purposes. The blockchain is mining and has two users, now I would like to subscribe to events in the blockchain and perform some actions. The code is below:

var Web3 = require("web3");     

var ether_port = 'http://localhost:8545'

var web3       = new Web3(new Web3.providers.HttpProvider(ether_port));

web3.eth.subscribe("pendingTransactions"

                  , function(err, result){

    if (err){ console.log(err) }

    else { console.log("result: ", result) }

});

I get something like:

Error: The current provider doesn't support subscriptions: HttpProviderat Subscription. subscribe 

In some sense not surprising since when I do web3.eth.subscribe on the node.js console I get:

{ [Function] call: undefined }

Even though the documentation for web3-1.0.0 states the function can be used: https://web3js.readthedocs.io/en/1.0/web3-eth-subscribe.html.

So is this just a matter of documentation being out of sync with actual implementation? Am I using it wrong?

If it is not implemented, what is the best way to listen to changes in the chain? For example, if I want a real-time update of a user's account balance? That is aside from the naive implementation of a function that pings the chain every n fraction of a second.

1 Answer

0 votes
by (29.5k points)
edited by

As the error indicates pub/sub is not available over HTTP. However, you can use it over WS. So, the documentation you referenced is not 100% wrong, it just omits the provider portion of the code.

So, what you should try is starting your nodes using web socket connections (
geth --ws --wsport 8545 ..., assuming you're using geth), and change to a WebsocketProvider.Refer the following code

var Web3 = require("web3");    

var ether_port = 'ws://localhost:8545'
var web3       = new Web3(new Web3.providers.WebsocketProvider(ether_port));

web3.eth.subscribe("pendingTransactions"
                  , function(err, result){
    if (err){ console.log(err) }
    else { console.log("result: ", result) }
});

Give yourself time to explore the Blockchain field properly. Enroll in Blockchain Online Training now.

31k questions

32.4k answers

500 comments

693 users

Browse Categories

...