Reconnect use case - best practices / strategies?

391 views
Skip to first unread message

Angel Todorov

unread,
Oct 14, 2015, 7:20:05 AM10/14/15
to EasyRTC
Hi there,
I would like to properly process disconnect case in my client.
So I see, that on the client side, easyrtc has "setDisconnectListener", which fires up, once the connection is lost.

So my simple question is - if the connection is already joined to the room(s) and receives "OnDisconnect" message what is the best strategy / practice to get it back a live and joined?
One example I saw from SailsJS (since this is my backend framework) - once Sails discovers that the connection is lost it start re-connection attempts from client side until the client is either closed or connected again.

Is it possible to make similar approach in easyrtc part of my client and if it is "Yes", I would appreciate any code hint...

Regards,
Angel

Eric Davies

unread,
Oct 14, 2015, 2:03:01 PM10/14/15
to EasyRTC
You'd need to set up a cookie on the client so you recognized it as the same client on subsequent connections, and store client cookies on the server for a short time after disconnection in case they came back. And your server would need to keep track of who is connected to who so it could tell clients to reestablish their connections. I don't think it's impossible, but its more than a one-liner :-).

Angel Todorov

unread,
Oct 14, 2015, 3:11:01 PM10/14/15
to eas...@googlegroups.com
Hi Eric and thanks for answering me.
What I decided to do:
- create my own socket and set via "easyrtc.useThisSocketConnection"
- set both reconnect and disconnect listeners for the socket:
                setDisconnectListener: function (listener) {
                    if (socket) {
                        socket.on('disconnect', listener);
                    }
                },
                setReconnectListener: function (listener) {
                    if (socket) {
                        socket.on('reconnect', listener);
                    }
                }
- when socket disconnects, listener is called which just shows client side notification that the socket is disconnected
                    var disconnectListener = function () {
                        $log.warn("Connection %s to the server lost! Web socket connected is %s", easyrtc.myEasyrtcid, easyrtc.webSocketConnected);
                    };
- when socket reconnects, listener is called, I start again easyrtc connection steps:
      + easyrtc.connect
      + easyrtc.joinRoom
The problem I experience is that, I have got "MSG_REJECT_BAD_ROOM", "Message rejected. Requested room is invalid or does not exist.", during easyrtc.connect.
Any idea why this happened?

Just kindly reminder - few months ago you provided me with some fix for "easyrtc.useThisSocketConnection", please refer here this User Supplied Socket.io demo & Socket.io 1.X support

Best Regards,
Angel

Angel Todorov

unread,
Oct 15, 2015, 4:59:42 AM10/15/15
to EasyRTC
Hi there,
After some additional digging, I discovered that when the socket is reconnected using the above approach, and other connection sends peer message to the reconnected peer, the message is received twice, e.i. it is somehow duplicated....

Eric Davies

unread,
Oct 15, 2015, 11:01:22 AM10/15/15
to EasyRTC
Interesting. If you send me a minimal demonstration case (some code I can put int my static directory and simply run), I'll look into it.

Angel Todorov

unread,
Oct 16, 2015, 2:29:10 PM10/16/15
to EasyRTC
Hi Eric,
It seems that I have some general issue in my project socket usage.

I have SailsJS on the backend and since it utilizes socket library, I decided to use SailsJS socket. It is quite complicated to provide you with simple to run code, I will try to explain you the case.
The issue I currently found is that after the connection broke and restores, easyrtc client stop on OnConnect and didn't not send "easyrtcAuth" to the easyrtc server. In that case, connections cannot re-join the room.

I can see in simple easyrtc sample, if the connections are connected to the server and joined to the room, even if they lose connection the server and restored, each connection automatically connects, authenticates and finally joins the room.

In my case, the restore process stops on first step - connect - I see this in easyrtc server log.

I would appreciate any help, since it is crucial for my project.

Thanks in advance,
Angel

Eric Davies

unread,
Oct 16, 2015, 6:51:14 PM10/16/15
to EasyRTC
Hi Angel,

The reason that i say, "supply me with minimal demonstrating code" is that we have one guy (me) that looks at easyrtc.js client issues, and one guy is already pretty busy.

However, here is a hypothesis:
  • when you connect, event listeners are set up on your socket.
  • when you normally disconnect, those listeners are lost with the socket because a fresh socket is allocated.
  • however, in your case, since you are managing your own socket, it's quite likely the socket is getting reused, which could mean you are getting multiple listeners.
   
Try the following: when you disconnect, remove the event listeners that easyrtc has added to the socket. Fortunately, easyrtc keeps a list of them which makes this easy.
     var i;  
     for (i in easyrtc.websocketListeners) {
                if (!easyrtc.websocketListeners.hasOwnProperty(i)) {
                    continue;
                }
                easyrtc.webSocket.removeEventListener(easyrtc.websocketListeners[i].event,
                        easyrtc.websocketListeners[i].handler);
    }



Angel Todorov

unread,
Oct 18, 2015, 8:35:13 AM10/18/15
to eas...@googlegroups.com
Hi Eric,
First of all - thanks for your dedication and will to help even in such cases...
Since I still struggling with the case, I prepared small example for you - please see enclosed archive.
This is npm package, so all you need to do is to extract it in particular folder, "cd easysails" and run "sudo npm install". It has some issues (my intention was to make it ASAP), so if errors occur - just run it one more time.
The package assumes you already have mongoDB and sails npm package installed globally.
After successful installation, just type "sudo sails lift" - it all start the server on port 1337
You will have two locations:
1) http://localhost:1337/auto - this works fine - you can see steps description in the page
2) http://localhost:1337/man -this is problematic case - again you can see the step description into the page
So I am in the second case and this is how I can reproduce it in separate project.

The entire client "code" is in easysails/assets/js/app.js file, views are in easysails/views/ folder (manpage.ejs & autopage.ejs files)

Hope this helps you find the solution.

Best Regards,
Angel
easysails.zip

Eric Davies

unread,
Oct 20, 2015, 12:40:51 AM10/20/15
to EasyRTC
Looking into it!
Reply all
Reply to author
Forward
0 new messages