janpantel / angular-sails

An angular module for using the sails socket.io api
MIT License
307 stars 56 forks source link

Feature Request - Ping the server or be able to set a timeout #77

Closed Fr33maan closed 9 years ago

Fr33maan commented 9 years ago

Hi,

I have a problem when the server is down, i not get any error message. Maybe you could implement something to check that the server is up ?

Bye

TheSharpieOne commented 9 years ago

socket.io, which is the underlining library that manages the socket connections, handles this and they already have that feature. socket.io has heartbeats as well as events that can be subscribed to in order to be informed of connection timeouts, as well reconnect attempts. You can find more information about the events and configuration of socket.io here: http://socket.io/docs/client-api/#client-api.

Fr33maan commented 9 years ago

Thanks for the ressources but if i have to check separately if the connection is alive, this is not what i'm looking for.

According to the docs you gave, the timeout is 20s. If i understand correctly, the alert won't be called after 20s if the URL does not exists ?

    $sails.get("/bars")
      .then(function(resp){
          $scope.bars = resp.data;
      }, function(resp){
        alert('Houston, we got a problem!');
      });
TheSharpieOne commented 9 years ago

Yes, the default is 20 seconds. And no, that is not for when a url does not exist. Your server should be returning 404s for nonexistent urls and should return them a lot faster. The 20 seconds is for when the connection is being opened and your server doesn't respond. Also if the browser determines there is an error before the 20 seconds (such as no Internet, connection reset, could not resolve host or something else) it will be triggered then and depending on your console settings, it will be logged there.

Fr33maan commented 9 years ago

Sorry when i said "url does not exists" i meant "url that goes nowhere -> to an inexistent server -> the server is down", it was very confusing and really not well said.

When the server is down, i can't see any request done with $sails in the console nor in the chrome's network tab. If the server is up, i can't see them too.

In the console i just see that the __getcookie request from sails.io.js is refused.

I know that the $sails.get is called but i can't see it nor see the expected result (timeout or could not resolve host name).

Should i rely on __getcookie ?

Sorry for all thoses questions

TheSharpieOne commented 9 years ago

Yes, in that case you should probably rely on __getcookie. The 1.x code relies on balderdashy/sails.io.js, and you can see here that if that cookie call fails, it never connects a socket. So in that case, all of the socket configs never come into play and the actual socket connection is never even attempted (which is why you do not seem them in your console.)

Fr33maan commented 9 years ago

Ok so there is no natural way to have an error with $sails.get if the server is offline because the socket connection will be never attempted ?

If the socket connection is not attempted i suppose that i can't use isConnected()?

I must admit that i don't really know how to use __getcookie to know if server is alive.

TheSharpieOne commented 9 years ago

You can use isConnected to determine if you are connected, but it will not inform you when the connection fails (you will have to poll it or $watch it). You also run into the risk of isConnected being false before the socket connects (while the get cookie stuff is in flight) and while the socket is trying to be connected. You can also check if the socket exists directly (if it does, that means the get cookie worked). This would differ from isConnected as it would let you know if the socket connection is at least in flight or being attempted. if ($sails._raw) {...}. You'd probably want to do this after a timeout. If the socket doesn't exist after x seconds... you'd likely display an error letting the user know that the server is offline or not responding.

Fr33maan commented 9 years ago

Thanks for your anwser. I ran into the isConnected() = false problem you are mentionning and I've implemented a timeout at the first connection in my app. If the request does not success, we ask to reload the app and say that the server is offline.

But I think that my feature request is not so much invalid because IMHO, if the socket can't connect and does not throw an error, this is a silent failing.

Thank you for all your precisions, I understand better how this is working.

TheSharpieOne commented 9 years ago

Perhaps you should open this issue with balderdashy/sails.io.js.