What can be the best way to get latest block data from geth ? I mean like can i use a script which gets the latest block automatically as soon as geth gets that latest block. My purpose is to check the latest incoming transaction ids inside each block. Thanks!


1 Answer 1


You can use web3.eth.filter to apply a filter and then watch the filter for changes.

The one line code will be:

eth.filter("latest").watch(function(error, result) {console.log(result)});

You can apply filter as per your choice, like if you want to scrap all the blocks just use:

var filter = web3.eth.filter({fromBlock:0, toBlock: 'latest', address: account});
filter.watch(function(error, result){if(!error console.log(result)});
  • Hi i tried the above solution but i am getting this error : web3.eth.filter is not a function
    – Farhan
    Feb 6, 2018 at 7:20
  • I am using this code please help : const express = require("express"); const app = express(); const Web3 = require("web3"); const web3 = new Web3("ws://"); var filter = web3.eth.filter('latest'); var filter = web3.eth.filter(options); filter.watch(function(error, result){ if (!error) console.log(result); }); web3.eth.filter(options, function(error, result){ if (!error) console.log(result); }); app.listen(3000);
    – Farhan
    Feb 6, 2018 at 7:21
  • What version of web3 are you using? If you are using 1.0 (in beta), you need to look at subscribe. Note that subscribe will not work with an HTTP Provider, only websocket and IPC Feb 6, 2018 at 7:45
  • I am using these versions : "web3": "^1.0.0-beta.29", "web3-eth": "^1.0.0-beta.29" , which version should i install to use filter function ? Also please tell me how to install the newer version, i was able to find only one option to install web3js : npm install web3
    – Farhan
    Feb 6, 2018 at 8:26
  • web3 1.0 is latest but in beta phase. filter was available in web3 0.2. So you are already using the latest version. Since both versions are quite different in the way they are implemented. So if you are using latest version , use subscribe instead of filter. Feb 6, 2018 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.