I am using web3j to listen to transactions and blockchains through geth. It seems that none of the subscriptions that I have created to the blockchain or transaction are detecting changes.

I start geth like like the following.

./geth \
 --fast --cache=512 \
 --rpc --rpcapi personal,db,eth,net,web3 \
 --ws --wsapi personal,db,eth,net,web3 \
 --metrics --verbosity 5 --fakepow

My Java code looks like the following.

public class App {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
    Web3j web3 = Web3j.build(new HttpService(), 100, Async.defaultExecutorService());  // defaults to http://localhost:8545/
    Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
    String clientVersion = web3ClientVersion.getWeb3ClientVersion();

    web3.blockObservable(false).subscribe(block -> {
    }, Throwable::printStackTrace, () -> System.out.println("block done"));

    web3.transactionObservable().subscribe(tx -> {
      System.out.println(tx.getHash() + ", " + tx.getGasPriceRaw());
    }, Throwable::printStackTrace, () -> System.out.println("tx done"));

    web3.pendingTransactionObservable().subscribe(tx -> {
      System.out.println(tx.getHash() + ", " + tx.getGasPriceRaw());
    }, Throwable::printStackTrace, () -> System.out.println("ptx done"));

    System.out.println("umm reached the end");

All I see on the console of my Java app is the following.

umm reached the end

Any ideas what I am doing wrong?


It seems that you forgot to add sleep statement. Remember, that subscription takes place asynchronously in a different thread of execution to the rest of our program. Also don't forget to make an unsubscribe method call. Hope it helps!


You need to connect to Geth socket and not HTTP. As you have not provided the socket URL while Web3J.Build, it will map to default values, as per your comment in the code.

Here's the example:

WebSocketService socketService=new WebSocketService("ws://", true);
Web3J web3Socket=Web3J.build(socketService);

8546 is default socket port for Geth. If you have changed that while starting Geth, make sure you use that.

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.