Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io packets get lost between dis- and reconnect

I am using socket.io for connecting a smartphone (in a webframe) to a server and sending a few messages around (a few short strings per minute, nothing big). Since smartphones often tend to have an unsteady connection, socket.io is forced to reconnect every now and then. It does this automatically and the messages I want it to send after it noticed that the connection is currently unavailable are buffered and sent as a bundle once the connection is reestablished. So basically it's all going well with socket.io.

But when I send a message right before socket.io notices that the connection is gone, the message is lost. It can not be transmitted, but it does not get buffered by socket.io, either. I am quite content with socket.io, but this problem is bugging me, since I do not send many messages, but I really need those I DO send to be sent reliably. While the connection is established, all messages get transmitted perfectly, so I do not need to check for that. Only the second between the connection loss and socket.io noticing it is the critical moment where I face packet loss.

I have been thinking about mimicking TCP behaviour with sequence numbers and ACKs but it feels a bit like shooting ants with a rocket launcher. So my question is: Does anyone have a lightweight solution for this problem or even encountered it himself? I do not even ask for explicit code - I can do that myself - but a concept or something like that to get rid of this specific problem would be great.

Thanks in advance!

like image 254
Jens Neumann Avatar asked Oct 07 '13 16:10

Jens Neumann


1 Answers

Why don't you just log every message to database? And use socket.io acknowledge option to make it even better.

from http://socket.io/#how-to-use SERVER

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.on('ferret', function (name, fn) {
    fn('woot');
  });
});

CLIENT

<script>
  var socket = io.connect(); // TIP: .connect with no args does auto-discovery
  socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!
    socket.emit('ferret', 'tobi', function (data) {
      console.log(data); // data will be 'woot'
    });
  });
</script>
like image 83
Dslayer Avatar answered Oct 19 '22 06:10

Dslayer