Ç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,430 points

edited Jun 19, 2018 by nsv999 369 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,090 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,600 points
111 views
+1 vote
1 answer
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,810 points
188 views
+1 vote
1 answer
0 votes
1 answer
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 26, 2018 in Blockchain by Perry
• 17,090 points
126 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,090 points
1,179 views