6

I'm developing an application where clients connect to a nodejs server via Socket.io and subscribe to a variety of events. These subscriptions are fairly complex can not be handled with Socket.IO's channel feature.

This means that the client needs to keep track of its subscriptions and may have to re-subscribe when it was disconnected. Unfortunately, I'm not quite sure how Socket.IO handles reconnecting and exactly how transparent that happens to the client.

So here's the question: how can I simulate a connection failure and force Socket.IO to reconnect?

n3rd
  • 5,989
  • 4
  • 39
  • 56
  • 4
    Unplug the Ethernet cable and plug it in again? – xavierm02 Aug 29 '11 at 10:42
  • Or maybe just delete the connection on either side without closing it before to see how the other side reacts. – xavierm02 Aug 29 '11 at 10:43
  • @xavierm02: That would work, but really isn't the kind of approach you'd want to use in unit tests :-) And how do I "just delete" the connection? – n3rd Aug 29 '11 at 11:33
  • Well you should have some kind of API that creates an object. And that object must somehow have a close method. Truc deleting the object without using that close method and perhaps you'll have a bug. If if there is a bug on one end, then the other hand will get the error you need. – xavierm02 Aug 30 '11 at 15:38
  • No I don't have a close method because the connection is not intended to be closed. And even if I did, I need to simulate this on the Socket.IO level because I want Socket.IO to reconnect automatically. – n3rd Aug 31 '11 at 07:01

2 Answers2

0

Socket.io is providing you events in your socket object. Actually, you can find various tools by reading their API.

See this exemple on stackoverflow Socket.IO handling disconnect event

Community
  • 1
  • 1
Fanghornn
  • 56
  • 3
-2

From my experience, I found this to be the easiest and useful solution:

Client side:

// the next 3 functions will be fired automatically on a disconnect.
// the disconnect (the first function) is not required, but you know, 
// you can use it make some other good stuff.

socket.on("disconnect", function() {
  console.log("Disconnected");
});

socket.on("reconnect", function() {
  // do not rejoin from here, since the socket.id token and/or rooms are still
  // not available.
  console.log("Reconnecting");
});

socket.on("connect", function() {
  // thats the key line, now register to the room you want.
  // info about the required rooms (if its not as simple as my 
  // example) could easily be reached via a DB connection. It worth it.
  socket.emit("registerToRoom", $scope.user.phone);
});

Server side:

io.on('connection', function(socket){
  socket.on("registerToRoom", function(userPhone) {   
    socket.join(userPhone);   
  });
});

And thats it. Very simple and straight forward.

You also can add in the connected socket (the last function) some more updates to the user display, such as refreshing its index or something else.

Raz
  • 1,910
  • 1
  • 15
  • 28