web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

How to subscribe on blockchain events (web3)?

Open maxprof opened this issue 6 years ago • 4 comments

Hi, I have a problem with subscribing to blockchain events on web3.js library.

` const Web3 = require("web3"); const web3 = new Web3(new Web3.providers.WebsocketProvider("wss:my_link"));

web3.eth.subscribe("pendingTransactions", (err, result) => { if (err){ console.log(err) } else { console.log("result: ", result) } }); ` But as a result, I got an Error message - "error: no "newPendingTransactions" subscription in eth namespace". How can I fix this?

Chacked on many web3 versions, at this moment - 1.2.0

Thanks for attention.

maxprof avatar Mar 18 '20 16:03 maxprof

Hi @maxprof. I have not been able to reproduce this on

  • geth (local)
  • ganache-cli (local)
  • Infura (remote over wss)

Using the following script with Web3 1.2.6

Index.js

const Web3 = require('web3');
const util = require('util');

async function main(){

  return new Promise(async function(resolve, reject){

    const web3 = new Web3('wss://mainnet.infura.io/ws/v3/<my-infura-projectID>');

    web3.eth.subscribe("pendingTransactions", (err, result) => {
      if (err) return reject(err);

      console.log(util.inspect(result));
      resolve(result);
    });
  });
}

main()
  .then(() => process.exit(0))
  .catch(err => {
    console.log(err);
    process.exit(1);
  })

Run

node index.js
> '0x2a1125b230f14d8c3f81f4287459bd67b59928493b2f45926738034279ed849f'

Which ethereum client / remote provider are you using in wss:my_link? It's possible they don't support the pending transactions endpoint.

cgewecke avatar Mar 19 '20 17:03 cgewecke

@maxprof Closing because it's been a few days without response here and things seem like they're working as expected on Web3's side.

Please ping with more info if you'd like this re-opened.

cgewecke avatar Mar 23 '20 02:03 cgewecke

i am facing this issue @cg

@maxprof Closing because it's been a few days without response here and things seem like they're working as expected on Web3's side.

Please ping with more info if you'd like this re-opened.

i am facing this issue @cgewecke did you manage to resolve this

D3G3Nerator avatar Aug 18 '21 11:08 D3G3Nerator

I'm facing the same issue with private Geth node, anyone have a solution for this.

DoHaiSon avatar Dec 20 '22 19:12 DoHaiSon