annot implement web3 eth subscribe on Web3 version 1 0 0-beta 27

0 votes

I am running a private ethereum blockchain on web3 version 1.0.0-beta.27 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 am getting the following error :

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

Kindly Help.

Jun 19, 2018 in Blockchain by nsv999
• 5,500 points

edited Jun 19, 2018 by nsv999 2,065 views

1 answer to this question.

0 votes

As the error suggests, pub/sub is not available over HTTP. However, you can use it over WS. 

This documentation -> https://web3js.readthedocs.io/en/1.0/web3-eth-subscribe.html states that we can use the function, but it omits the provider portion of the code.

Try starting your nodes using web socket connections (geth --ws --wsport 8545 ..., assuming you're using geth), and change to a WebsocketProvider.

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) }
});
answered Jun 19, 2018 by Perry
• 17,100 points

edited Jun 19, 2018 by Perry

Related Questions In Blockchain

0 votes
1 answer

Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation?

The returned bytestring is the deployedBytecode of the contract, ...READ MORE

answered Sep 17, 2018 in Blockchain by digger
• 26,740 points
1,239 views
0 votes
1 answer
+1 vote
2 answers

Challenge while setting up Hyperledger Fabric 1.0 in Ubuntu 16.04

The rocksdb error you're seeing wouldn't be ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points

edited Jun 8, 2020 by Sirajul 1,175 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,956 views
+1 vote
1 answer
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,483 views
0 votes
1 answer

Error while running make command using Fabric 1.0.6

It's a known issue in go 1.9.4: https://github.com/golang/go/issues/23739 While ...READ MORE

answered Jun 27, 2018 in Blockchain by Perry
• 17,100 points
1,114 views
0 votes
4 answers

Issue with getting web3.eth.accounts (MetaMask)

I solved problem, using: web3.eth.getAccounts((err, res) => ...READ MORE

answered Oct 12, 2018 in Blockchain by Perry
• 17,100 points
5,478 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP