Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pusher doesn't reconnect when network is disconnected for ~5 min #210

Open
arslanbhutta opened this issue Jul 29, 2019 · 4 comments
Open
Labels

Comments

@arslanbhutta
Copy link

Pusher doesn't reconnect when network is disconnected for ~5 min.

Steps to reproduce:

Connect to Pusher and subscribe to channel. Events are received as usual.
Turn off Wifi for ~5 min.
Turn on Wifi.
What I can observe is that there is a Disconnection message in the web console once Wifi is turned on, but in the app, pusher.connection.connectionState still returns connected and pusher.connection.socketId returns the disconnected(as seen in the web console) socket id.
It works fine if Wifi is turned off for less than 2-3 min - it auto reconnects and receives events again, it's only an issue when it's disconnected for ~5 min or more.

I've also added

    pusher.connection.reconnectAttemptsMax = nil
    pusher.connection.maxReconnectGapInSeconds = 60

but it doesn't change anything.

I've also added a listener on changedConnectionState, but the only time it fires is during the initial connection setup - it goes from connecting to connected.

@WillSewell
Copy link
Contributor

I'm concerned that reconnectAttemptsMax is the wrong property name. Could you try setting:

PusherOptions options = new PusherOptions().setMaxReconnectionAttempts(1000000);
Pusher pusher = new Pusher(YOUR_APP_KEY, options);

@nros
Copy link

nros commented Jan 16, 2020

The reconnection logic might not working for total network break-down beyond 60 seconds, and some special conditions.

Cause

Setting max reconnection attempts along max reconnection gap works, though:

    PusherOptions options = new PusherOptions()
                .setCluster("eu")
                .setMaxReconnectionAttempts(Integer.MAX_VALUE)
                .setMaxReconnectGapInSeconds(30)
    ;

Nevertheless reconnecting fails after network is gone for 60 seconds, because the DNS TTL turned the host name IP resolution invalid, leading to an exception, making the Pusher websocket handler bail out.

An Pusher exception occured: An exception was thrown by the websocket [null]
java.net.UnknownHostException: ws-eu.pusher.com
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:606)
	at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:666)
	at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:395)
	at java.lang.Thread.run(Thread.java:748)

This is, what DNS is giving me at this moment for the hostname ws-eu.pusher.com.

$ dig ws-eu.pusher.com

; <<>> DiG 9.10.6 <<>> ws-eu.pusher.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56905
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1452
;; QUESTION SECTION:
;ws-eu.pusher.com.		IN	A

;; ANSWER SECTION:
ws-eu.pusher.com.	3600	IN	CNAME	eu-ws-1607514969.eu-west-1.elb.amazonaws.com.
eu-ws-1607514969.eu-west-1.elb.amazonaws.com. 60 IN A 63.34.139.38
eu-ws-1607514969.eu-west-1.elb.amazonaws.com. 60 IN A 34.251.148.235
eu-ws-1607514969.eu-west-1.elb.amazonaws.com. 60 IN A 52.16.80.29
eu-ws-1607514969.eu-west-1.elb.amazonaws.com. 60 IN A 52.18.181.231

;; Query time: 20 msec
;; SERVER: 172.16.160.1#53(172.16.160.1)
;; WHEN: Thu Jan 16 11:12:10 CET 2020
;; MSG SIZE  rcvd: 164

Although the CNAME value for the host is valid for 3600 seconds (checked the max value by querying Amazon DNS directly!), the CNAME targets are only valid for 60 seconds.

So after 60 seconds, no IP address can be resolved and Pusher websocket handler fails with the mentioned exception.

Mitigation

Sometimes, Pusher library continued to try reconnection, sometimes it did not, which i need to continue investigating.

What I did in my code, was, to add a connection listener, handling this special case. Upon first successful connection, a flag will indicate that the host name is valid in principle. If an UnknownHostException exception is received, then the exception is ignored but instead Pusher instance is called to connect again pusher.connect(). This seems to work.

@amir20001
Copy link

Would love to see this mitigation built directly into the library.

@ben-j69
Copy link

ben-j69 commented Jun 1, 2022

Having an issue that might be the same in my app : Unable to resolve host "ws-eu.pusher.com": No address associated with hostname
The default value of maxReconnectionAttempts is only 6 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants