0

I'm using Socket.IO (the latest version 1.1.0) to exchange messages with an Android app (the client). I would like to set a timeout (5s for example) to check if my client is still connected or not (I would like to handle the case when the Android app crashes). Moreover, I would like to generate an event when a this timeout occurs. What I want to do looks like this:

1/ Set the timeout

var socket = require('socket.io')({
   //options go here
  'timeout': 5000 //set the timeout to 5s
});

2/ Handle timeout event:

socket.on('timeout', function(){
   //my treatment
});

But I don't find any implementation to handle the timeout.

Thanks for your help !

rdroid
  • 43
  • 1
  • 5

1 Answers1

3

That's not what the timeout setting handles. The timeout is how long the server will wait for a re-connect before it tears down that connection. You probably don't want to set it that short; possibly even leave it at the default.

Socketio automatically sends heartbeats by default. You really should just need to assign a function to run when the 'disconnect' message is received on the server side.

io.on('connection', function(socket){
       console.log('a user connected');
       socket.on('disconnect', function(){
           console.log('user disconnected');
       });
});

Check out this post for more information about the heartbeat: Advantage/disadvantage of using socketio heartbeats

Community
  • 1
  • 1
HeadCode
  • 2,770
  • 1
  • 14
  • 26