WSS Connection Timeout Randomly


I have integrated Infura for ETH deposits and Withdrawal functionality in a centralized exchange project. All functionalities are working fine, except the subscription, which is required to track incoming ETH deposits of the users.

Below is code snippet:

WebSocketService socketService = new WebSocketService("wss://<myPID>", true);

public void start() throws ConnectException, OnErrorNotImplementedException {"Started >>>>>>> ");

    Web3j web3jSocket =;

    Disposable subscription=web3jSocket.transactionFlowable()
            .subscribe(tx ->
      "Tx Hash > " +tx.getHash());

            }, throwable -> {
      "Message >>>> "+throwable.getMessage());

Problem is that, it randomly throws connection timeout exception. I am currently on a high-speed lease line so I am sure connectivity issue isn’t from my end.

I have been struggling from three straight nights on this part. Without this, exchange launch is impossible.

Below is the exception :

2020-05-31 17:36:50.686 INFO 14044 — [ool-14-thread-2] : Tx Hash > 0x9f13976bc3ec920084b75562f04ff3961f1e9ff9cecd6579d0e738fe17f32697
2020-05-31 17:36:51.996 INFO 14044 — [ool-14-thread-2] : Tx Hash > 0x06314762d33a287b97446d8b8ffe22324a8ac88e8ca9734d33a835d891bf525a
2020-05-31 17:36:52.519 INFO 14044 — [ool-14-thread-2] : Tx Hash > 0xe276b29e8b8fcfad3e12bf7f6ee351b0bd9e7c20d815552925118efd2593500c
2020-05-31 17:36:54.161 INFO 14044 — [ool-14-thread-2] : Tx Hash > 0x6cdb62a39847f7c3ea7a4614a95eb13ca28fdfcd70d3107bc1e7a0c220f9991d
2020-05-31 17:36:54.658 INFO 14044 — [ool-14-thread-2] : Tx Hash > 0x47b7ee687a21294a2b46c896e3480708abb915518bfa6d3cd3340ef1d9efa043
2020-05-31 17:37:05.129 INFO 14044 — [ool-14-thread-2] : Message >>>> timeout timeout
at okio.Okio$4.newTimeoutException(
at okio.AsyncTimeout.exit(
at okio.AsyncTimeout$
at okio.RealBufferedSource.indexOf(
at okio.RealBufferedSource.readUtf8LineStrict(
at okio.RealBufferedSource.readUtf8LineStrict(
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(
at okhttp3.internal.http.CallServerInterceptor.intercept(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.internal.connection.ConnectInterceptor.intercept(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.internal.cache.CacheInterceptor.intercept(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.internal.http.BridgeInterceptor.intercept(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.internal.http.RealInterceptorChain.proceed(
at okhttp3.RealCall.getResponseWithInterceptorChain(
at okhttp3.RealCall.execute(
at org.web3j.protocol.http.HttpService.performIO(
at org.web3j.protocol.Service.send(
at org.web3j.protocol.core.Request.send(
at io.reactivex.internal.subscribers.LambdaSubscriber.onNext(
at io.reactivex.internal.operators.flowable.FlowableFlattenIterable$FlattenIterableSubscriber.drain(
at io.reactivex.internal.operators.flowable.FlowableFlattenIterable$FlattenIterableSubscriber.onNext(
at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.tryEmitScalar(
at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.onNext(
at io.reactivex.internal.operators.flowable.FlowableCreate$BufferAsyncEmitter.drain(
at io.reactivex.internal.operators.flowable.FlowableCreate$BufferAsyncEmitter.onNext(
at org.web3j.protocol.core.filters.BlockFilter.process(
at org.web3j.protocol.core.filters.Filter.pollFilter(
at org.web3j.protocol.core.filters.Filter.lambda$run$0(
at java.util.concurrent.Executors$
at java.util.concurrent.FutureTask.runAndReset(
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(
at java.util.concurrent.ScheduledThreadPoolExecutor$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
Caused by: Socket closed
at okio.Okio$
at okio.AsyncTimeout$
… 42 more

Can someone help ?


Hello @Kunal_Thakker, I’ve done some research into this and I’ll try to help out. After you build the web3j service, can you try

web3j.blockObservable(false).subscribe(block -> {
    System.out.println("NEW BLOCK -> " + block.getBlock().getNumber().intValue());

to see if you are able to get each new block as it comes in? Also, how long are you able to run your code without an exception? A minute? 10 minutes? Less?

Thanks for the reply.

I have digged a little deeper and found one more clue. Whether it is blockflowable or transactionflowable, exception issue is there. But I do not get any exception if I run the service using IDE, (IntelliJ)

But if I compile it to jar using mvn clean install, and mvn clean package, and then run it, it throws random exception at random times. Sometimes I get connection time out, sometimes websocketnotconnected exception.

I have posted the issue with Web3J community as well, and also searched on Google. there are many people facing this issue when they have infura and Web3J combined.

Okay hmm. 2 things, can you add in reconnect logic so that if you get a time out or websocketnotconnected it automatically reconnects?

And can you try using HttpService instead of WebsocketService?

Pardon my ignorance, can you guide how to reconnect and subsribe ? I tried researching a lot about OnError() but there isn’t enough documentation on how to use it.

For HTTPService, here’s the exception:

Caused by: java.lang.UnsupportedOperationException: Service HttpService does not support subscriptions

Besides, I received the reply from Web3J team for my issue:

@kunalbarchha yes I have seen related issue before - we concluded it was to do with Infura issue. I am not sure how to solve it because I cannot replicate.”

Hello. Have you fixed this bug?

Hi @Danyliuk_Mykola, and welcome to the Infura community! We have recently updated our WSS service to notify our users when a connection drops. The best solution is to have a reconnection set into your code so if the connection does drop, it will reconnect automatically.

Hello, could you possibly point to documentation or example of how to do that with a Web3j subsciprion? I have been googling for days and can’t seem to figure it out.