3

I'm using callbacks in Socket.IO to know when an action is finished. For example:

$loading.show();

socket.emit('delete', {id:123}, function(error){
    $loading.hide();

    if(error)
        alert(error);
});

As I understand it, if the socket is disconnected then the message is queued until the socket connects. Then it is sent. But this could be minutes or even hours, right?

Is there a way to clear a queued message, set a timeout, or otherwise prevent a message from sending if it's been waiting for say 20 seconds?

bendytree
  • 13,095
  • 11
  • 75
  • 91
  • Are you sure about that queue? I haven't really used Socket.io, but as far as I understand the concept each connection creates a new socket object, which has nothing to do with previously connected sockets. And messages belong to sockets, so there shouldn't be any old messages after a reconnect. – Sebastian vom Meer Jun 20 '13 at 20:46
  • I'm not sure & can't find any docs. At the very least, I know events are queued *before* connection happens because you can emit before connect. – bendytree Jun 20 '13 at 20:53

1 Answers1

3

There are pretty simple solution for this:

socket.on('connect', function() {
    socket.sendBuffer = [];
    // do stuff
});

The problem is that when socket lose connection there are may be some data left in socket buffer. So, just clear it up.

Oleksii Shnyra
  • 623
  • 1
  • 6
  • 24