Socket.io-client-java: Socket.IO-client.java disconnect and reconnect repeatedly

Created on 9 Jan 2015  Â·  26Comments  Â·  Source: socketio/socket.io-client-java

I am using socket.io 1.2.1 on my node server and the android socket connects with the server fine but after some minutes it automatically disconnect and reconnect repeatedly. Whats wrong with my code?
i am using socket.io-client-0.1.1.jar, engine.io-client-0.2.1.jar and Java-WebSocket-1.3.0.jar libraries.
here is the java code

    private void socketTest() throws URISyntaxException{
    socket = IO.socket("http://192.168.169.2:8082");
    socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

      @Override
      public void call(Object... args) {
        socket.emit("test", "awesome");

      }

    }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

      @Override
      public void call(Object... args) {}

    });
    socket.connect();

   }

and here is the server side code

  io.on('connection', function (socket) {
    console.log('a user connected');
    socket.on('disconnect', function () {
       console.log('user disconnected');
    });
    socket.on('test',function(msg){
       console.log("This is "+msg);
    });
   });

and the logs screenshot on the server

stack

Question link on stackoverflow

bug

Most helpful comment

I have the problem when in android phone, too

All 26 comments

I am experiencing the same... any help would be appreciated.

I think there is nothing wrong with your code specifically.. I think the problem lies at the lower levels.

I'd like to know the details.
Does this happen on the latest version? Is it sure that your internet connection is stable?

In my case, network connection is fine and I am using this project via @wf9a5m75 's socket-io cordova plugin and it seems to link to the latest code base. Thanks..

@nkzawa Also, this problem seems to be similar/same as the disconnect/reconnect issue at #60.

@nkzawa i am using socket.io-client-0.1.1.jar, engine.io-client-0.2.1.jar and Java-WebSocket-1.3.0.jar and its on local network, internet should not be the problem.

I have the problem when in android phone, too

@Miqe Chinese? have email? my email is [email protected]

I tried to reproduce the problem with a demo app, but couldn't.

Can you post source code and dump of logs if it's possible?

@nkzawa Ok, I tried to the demo app and it disconnects as well.. I cloned both your demo app and example chat.socket.io server source code and ran the server locally and point your demo app to my local server.

All I do to re-produce is that I just start the client and leave it like that.. after a few mins, it disconnects.

I added a log statement for disconnect and reconnect on the server side:

io.on('connection', function (socket) {
  console.log('a client connected ' + new Date);

  socket.on('disconnect', function () {
    console.log('a client disconnected ' + new Date);

Hence logs from server..

a client connected Tue Feb 17 2015 19:54:37 GMT+0000 (GMT)
a client disconnected Tue Feb 17 2015 19:56:02 GMT+0000 (GMT)

Let me know if I can do more to help diagnose the issue.

I left the app run itself without touching the phone at all and here what happens over a while:

a client connected Tue Feb 17 2015 19:54:37 GMT+0000 (GMT)
a client disconnected Tue Feb 17 2015 19:56:02 GMT+0000 (GMT)
a client connected Tue Feb 17 2015 20:31:12 GMT+0000 (GMT)
a client disconnected Tue Feb 17 2015 20:32:37 GMT+0000 (GMT)
a client connected Tue Feb 17 2015 20:56:28 GMT+0000 (GMT)
a client disconnected Tue Feb 17 2015 20:57:53 GMT+0000 (GMT)
a client connected Tue Feb 17 2015 21:02:29 GMT+0000 (GMT)
a client disconnected Tue Feb 17 2015 21:03:55 GMT+0000 (GMT)

And the client is not aware that it disconnected for a long time (until it actually connects again).. so disconnect event is not fired on the client side when server disconnects happens and log is printed.

There is a simple workaround for this, please refer to this:

https://github.com/nkzawa/socket.io-client.java/issues/123#issuecomment-96066333

@nkzawa Close this?

@shuoli84 Did you resolve this?. I still get problems.

@anop72 0.5.1?

@shuoli84 Yes compile ('com.github.nkzawa:socket.io-client:0.5.1')

@anop72 I don't have such problem. Share your client code? And also the socketio server's version? And any logs?

Here are my findings on this issue. I have two Android devices: an S3 running Kitkat (4.4.2) and a Tab 4 running Lollipop (5.0.2). Both are running my app using this socket.io client. Only the Tab 4 running Lollipop is affected by this issue.

The Tab 4 disconnects (as detected by the server upon heartbeat timeout) shortly after the Tab 4 goes to sleep while running on battery. The issue will not occur if it is plugged in.

My guess is that in order to continue squeezing better battery life, Lollipop is pausing what appears to be a dormant app and it times out on the server's heartbeat. Kitkat let the app continue to run to some extent if it had an open socket.

To remedy this, I added a wake_lock to my activity that I want to keep running to wait for a connection from a peer. To do this, I added the following to my activity:

private PowerManager.WakeLock wl;

OnCreate

    PowerManager pm = (PowerManager)getApplicationContext().getSystemService(
            Context.POWER_SERVICE);
    this.wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK
                    | PowerManager.ON_AFTER_RELEASE,
            TAG);
    wl.acquire();

OnDestroy

wl.release();

After adding this lock, I am no longer having this issue on Lollipop. In thinking about it, Kitkat should probably have paused my app too without a wake_lock but it did not.

Hope this helps.

I met the same problem ........

@Miqe Do you ues okhttp.jar in you App? I have the same question like you ,my socket disconnect and reconnect repeatedly,when I remove the okhttp.jar , it works well,connect once ,and no disconnect,i dont konw why it like this @nkzawa

@bwilks128, do you have a solution for a background service? Class that extends Service.

Hello guys, I know I am late to the party but I am here anyway. Was this ever resolved, coz its 2017 and I have an app based on Socket.IO and node but there are concerns with battery life and data consumption. If this was resolve could someone direct me to the solution so I can fix my code. Thanks.

Just Downgrade the socket.io-client:0.8.3 version in gradle its work fine for me

compile('io.socket:socket.io-client:0.8.3') { exclude group: 'org.json', module: 'json' }

hi @nkzawa & thank you for this nice project.
i think this issue is all about engine:polling,
if you limit transport to polling,you can see this problem

Although it's incredible, for socket.io v1.x, the best java client is 0.8.3 as mentioned by @Jethvasagar .

Use 0.8.3 version of io.socket remove new version 1.0.0

compile('io.socket:socket.io-client:0.8.3') {
    exclude group: 'org.json', module: 'json'
}

I've got the same issue. Doesn't work with 1.0.0 nor with 0.8.3 - it keeps disconnecting and reconnecting again :(

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pareshdevatval picture pareshdevatval  Â·  4Comments

AljoSt picture AljoSt  Â·  8Comments

noosxe picture noosxe  Â·  5Comments

emanuelet picture emanuelet  Â·  7Comments

yosuke-furukawa picture yosuke-furukawa  Â·  8Comments