kik-bot-api-unofficial icon indicating copy to clipboard operation
kik-bot-api-unofficial copied to clipboard

Sending `<ping/>`s for keep-alive and preventing disconnection

Open PreneeshAV opened this issue 6 years ago • 7 comments

When we run echo_bot.py and if internet gets disconnected during the process. The program hangs and doesnt reconnect.

PreneeshAV avatar Aug 20 '18 15:08 PreneeshAV

@tomer8007 Are you interested in ensuring that the API will try again to connect during the even of an internet outage, or would that be something that's left for the client controller to do? (i.e., echo_bot.py)

xorist avatar Dec 10 '18 23:12 xorist

I think this is a pretty low priority feature. I'd say we'll leave it for the client controller.

tomer8007 avatar Dec 10 '18 23:12 tomer8007

I agree, doesn't seem like something that should necessarily be included with a project like this. @preneesh If you need help writing a controller that will retry upon losing connection, I'm sure someone would be happy to help (considering you're still around haha, looking at the age of this issue)

xorist avatar Dec 10 '18 23:12 xorist

An auto reconnect may be out of the scope of the project. However the API should provide a way to do that in the client controller. If I see it correctly, the API doesn't support that yet. There isn't even a log message when I lose my internet connection.

If I understand it correctly (as an amateur in networking), there is a principal problem. There is no reliable way to detect a silently dropped connection immediately. However, we could use those keep-alive packets:

Happens every half hour. Disconnect after 10th time. Some kind of keep-alive? Let's send it back.

Unfortunately, in my experiments, I failed to cause a "pong" response to my initial "ping", which was b' '. Maybe only the server is supposed to send the pings. Nevertheless, we could still wait for incoming pings and reset a timer whenever a ping arrives. As soon as the age of the latest ping exceeds a predefined period of time (e.g. 45 minutes), we could invoke a callback method, e.g. on_ping_timeout. This should be a flexible and non-aggressive solution.

If you like this idea, note that I don't know when I could find the time to implement it by myself.

3dik avatar Aug 08 '20 23:08 3dik

Something this bot misses completely is the <ping/> stanza. It's only 7 bytes long and saves a lot of trouble. Send that every 10 seconds or so and watch if you get a <pong/> back. If you don't, it likely means the connection is broken and it's time to restart the connection. Also Kik expects you to do this because the app does it. It's an alternative solution to the XMPP whitespace proposal found here https://tools.ietf.org/html/rfc6120#section-4.6.1 and you're supposed to send it in intervals of less than 30 minutes (though I would keep it to intervals of 15 seconds or less for quality assurance).

By XMPP rules, sending a whitespace is NOT supposed to receive a whitespace back, so that's why <ping/> exists.

Also make sure if you're sending this, send it AFTER the <k initial connection stanza is okayed by the Kik server.

With this pinging method, you can keep a connection alive for days at a time with no issues, although that is not recommended

bluemods avatar Sep 11 '20 13:09 bluemods

An auto reconnect may be out of the scope of the project. However the API should provide a way to do that in the client controller. If I see it correctly, the API doesn't support that yet. There isn't even a log message when I lose my internet connection.

If I understand it correctly (as an amateur in networking), there is a principal problem. There is no reliable way to detect a silently dropped connection immediately. However, we could use those keep-alive packets:

Happens every half hour. Disconnect after 10th time. Some kind of keep-alive? Let's send it back.

Unfortunately, in my experiments, I failed to cause a "pong" response to my initial "ping", which was b' '. Maybe only the server is supposed to send the pings. Nevertheless, we could still wait for incoming pings and reset a timer whenever a ping arrives. As soon as the age of the latest ping exceeds a predefined period of time (e.g. 45 minutes), we could invoke a callback method, e.g. on_ping_timeout. This should be a flexible and non-aggressive solution.

If you like this idea, note that I don't know when I could find the time to implement it by myself.

I believe auto reconnect is definitely in the scope because messaging clients are supposed to stay online all the time to recieve and send messages, this isn't an Android app, we don't need to save battery.

In addition, the android app NEVER sends a single whitespace to keep alive, so I recommend removing that code immediately and replacing it with <ping/> instead at the very least.

bluemods avatar Sep 11 '20 14:09 bluemods

This sounds simple to implement. Also a relevant discussion for it is #211.

tomer8007 avatar Mar 20 '23 22:03 tomer8007