2

I use web3j in my Java application. In the smart contract, an event is defined.

 event sendEvent(string name);

I subscribed to that event in my app.

contract.sendEventEventFlowable(filter)
        .subscribe(doc -> {           
            System.out.println(doc.name);
        }, throwable -> {
            throwable.printStackTrace();
        }, () -> {
            System.out.println("End of research");
        });

It works, But I noticed that Web3j listens to the Blockchain every 15 seconds.

That is not suitable for my application and I need to listen to what is happening inside the blockchain all the time.

How can I change the default value (15 sec)?

This is my code

WebSocketService ws = new WebSocketService("ws://localhost:8545", true);
        ws.connect();
        web3 = Web3j.build(ws);
4
  • in the ethereum mainnet a block is generated each ~15 seconds .. between those blocks nothing happens
    – Majd TL
    Jun 26, 2022 at 11:23
  • I'm using ganache, but it works when I use the same code using web3js in JavaScript, in my opinion, is related to the web3j library. Jun 26, 2022 at 11:38
  • Webj does not know that you are using ganache. I think it is configurable but anyway that was my answer to why it is set to 15 seconds
    – Majd TL
    Jun 26, 2022 at 11:45
  • I agree with you, it is configurable, but how? Jun 26, 2022 at 11:50

1 Answer 1

2

On ethereum mainnet a block is generated each 15 seconds. that is why 15 is the default value for events. however you can change this by using this build method:

    /**
     * Construct a new Web3j instance.
     *
     * @param web3jService web3j service instance - i.e. HTTP or IPC
     * @param pollingInterval polling interval for responses from network nodes
     * @param scheduledExecutorService executor service to use for scheduled tasks. <strong>You are
     *     responsible for terminating this thread pool</strong>
     * @return new Web3j instance
     */
    static Web3j build(
            Web3jService web3jService,
            long pollingInterval,
            ScheduledExecutorService scheduledExecutorService) {
        return new JsonRpc2_0Web3j(web3jService, pollingInterval, scheduledExecutorService);
    }

I found this in this issue about slow events https://github.com/web3j/web3j/issues/233 and the answer referenced to this method and link https://github.com/web3j/web3j/blob/master/core/src/main/java/org/web3j/protocol/Web3j.java#L17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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