socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.32k stars 972 forks source link

socket.io timeout problem Android #230

Closed MaxHastings closed 3 years ago

MaxHastings commented 8 years ago

So it seems that whenever the Android phone goes to sleep long enough for the server its connected to timeout the client, that this problem occurs. The client wakes up and continues trying to reconnect to the server who is now ignoring the client since it already forcibly timed it out (I believe this is where the problem could be fixed if the server told the client hey I killed you already instead of letting the client continue being stupid). The client keeps trying for 60 seconds until finally giving up and establishing a new connection. The only solution that nkzawa and others have said is to acquire a wakeLock and wifiLock, which I think is a bad idea since my app runs in a background service 24/7 on the users phone. Acquiring a wakeLock would prevent the phone from sleeping causing bad battery drainage. Is there any other way to solve this issue?

nkzawa commented 8 years ago

wakeLock is a solution when you want to keep connection even if a device tried to go sleeping. Otherwise I think you can just close and reestablish connection when it required (like Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON).

MaxHastings commented 8 years ago

I feel disconnecting and connecting to my server every time the screen turns on isn't a proper solution either. Other apps are able to maintain what seems to be a constant connection without aquring a wakeLock 24/7 since my phone still goes to sleep.

Is there a way to do wakeLock at intervals to check with the server to see if new messages are waiting (chat app). I think apps like Facebook, Snapchat, and Kik do this.

nkzawa commented 8 years ago

How about http://developer.android.com/training/scheduling/alarms.html . Anyway, I think the problem should be in what kind of intervals you make connection open/close.

fatihsokmen commented 8 years ago

@maxwell10206 @nkzawa When device is not connected, best way to reach device is push notification. Setting an alarm is not a good practice to wake up device periodically.

MaxHastings commented 8 years ago

@fatihsokmen Are you referring to GCM push notifications? Could you explain what you mean? Also I believe many apps such as Facebook and instant messengers use the alarm method to query the server periodically.

fatihsokmen commented 8 years ago

@maxwell10206 Yes GCM, most of app uses alarms to wake up and sync some data (but not so frequent), but this can not be applied for a real time chat app. Let's talk about an alarm period of 5 or 10 seconds . This will never let device go on sleep. You may try this on your own device, It will quickly drain host device battery. Finally it is your design choice but not a good practice. Take a look at this. http://developer.android.com/training/scheduling/wakelock.html#screen

MaxHastings commented 8 years ago

@fatihsokmen GCM would replace socket.io, which I don't want. I have already built my entire app using the socket.io library. I don't plan to implement the alarm idea but I was forced to have my client reconnect to the server every time the phone screen turns on.

fatihsokmen commented 8 years ago

@maxwell10206 Sorry if I mislead you. I don't mean replacing socket.io with GCM. They are not rival but complementary. Good luck.

miladr0 commented 6 years ago

@maxwell1020 Did you solve this problem? I have same your problem

MaxHastings commented 6 years ago

@miladr0 Yeah. On my next project I ended up using Google Firebase for push notifications and just simple HTTP calls to a REST API to fetch data from the backend server.

miladr0 commented 6 years ago

@maxwell10206 i found startForeground, because i'm using socket.io inside a service. but socket.io has many problem for example phone get heated or drain battery or connect and reconnect so many even on lasted version. i never ever suggest this library maybe uWebSockets is better for my next project. thanks anyway

MaxHastings commented 6 years ago

@miladr0 Sockets require a constant connection to a server and use pings to maintain that connection. Which is why the battery will drain quickly, cause you need to prevent the phone from sleeping or else you will lose that connection. Socket.io is useful for real time data such as a video game where data is constantly being sent between client and server. For most applications you don't need to have a socket connection. I recommend just using simple HTTP requests and if you need push notifications use Firebase.

darrachequesne commented 3 years ago

Closed due to inactivity, please reopen if needed.