Android Question How do I keep a socket and websocket connection connected without interruption or disconnection ?

Waldemar Lima

Well-Known Member
Licensed User
hello everyone !!

How do I keep a socket and websocket connection connected without interruption or disconnection efficiently?

do I just need to ping X in X seconds to the server without the server responding to the client, or do I have to send the X in X second ping to the server and the server responds to the client?
 

drgottjr

Expert
Licensed User
Longtime User
technically you cannot prevent a disconnection. websockets were introduced to avoid your "ping" suggestion, which was one way to address the matter. the original http protocol built a disconnect in as part of the design. http 1.1 went the opposite way and allowed for the connection to stay open. but once a disconnect occurs, it's up to the client to try to make contact again. in the case of websockets, your client software might notice and inform you. you'd have to check the spec, but i think generally, you won't know until you try to talk to the server and time out on any response.
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
hello everyone !!

How do I keep a socket and websocket connection connected without interruption or disconnection efficiently?

do I just need to ping X in X seconds to the server without the server responding to the client, or do I have to send the X in X second ping to the server and the server responds to the client?

 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User

I am facing a problem with this "reconnect" in the authentication part of users with access levels ... :(
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
technically you cannot prevent a disconnection. websockets were introduced to avoid your "ping" suggestion, which was one way to address the matter. the original http protocol built a disconnect in as part of the design. http 1.1 went the opposite way and allowed for the connection to stay open. but once a disconnect occurs, it's up to the client to try to make contact again. in the case of websockets, your client software might notice and inform you. you'd have to check the spec, but i think generally, you won't know until you try to talk to the server and time out on any response.

but in the case of Socket TCP or UDP connections, would a ping in 1 second solve it?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
there is ping and there is ping. ping is an actual term for a specific protocol. because of hacking attempts, some servers no longer answer the so-called ping. if you mean can you simply ask your server "are you still there?", you can do that. that was what websockets is designed to avoid.
if you mean such a "ping" every second, what's the purpose? you'll spend more time pinging than communicating. also, the setup and breakdown for tcp for such an operation is very expensive. as for udp, you have to control both ends. just because you send some kind of message to a server (whether tcp or udp), the server has to know what's going on.

when you say
I am facing a problem with this "reconnect" in the authentication part of users with access levels ...
, you don't indicate what that problem is. but since you can't control a disconnect, i don't see how you can address your problem. tcp is pretty reliable (where would we be today without it?) if disconnects are a problem for you, this sounds more like a networking issue.

a lot of speculation, really, without understanding what your app does. look, think about a telephone call: 99.99% go through without issue. sometimes there are disconnects. you re-dial. if you're in the jungle with poor reception, it is what it is. you can't blame software for that.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
one other thing about ping (the *nix utility), while it may establish that there is something on the other end, it doesn't mean your socket connection is alive. it just means the server is up and responding to a ping. it's separate from whatever socket connection there may have been between your app and the server. ping (the utility) doesn't use tcp or udp. do you follow? it's like having 2 phones, 1 in each ear. the one in the right ear doesn't prove anything about what's going on in the left ear. 2 separate channels even though both may be calling the same number.

if you think polling the server during periods of inactivity is what you want to do to keep a connection open, you're welcome to do that, but it's not an actual "ping". you will also have to make sure the server knows what your polling is doing. and, again, if there is a disconnect for whatever reason, you cannot stop it.
websockets addresses all that work.
 
Upvote 0
Top