Forcing chat bot made with JAXL/XMPPHP to reconnect upon disconnection

1k views Asked by At

I'm using the JAXL library to implement a jabber chat bot written in php, which is then ran as a background process using the PHP CLI.

Things work quite well, but I've been having a hard time figuring out how to make the chat bot reconnect upon disconnection!

I notice when I leave it running over night sometimes it drops off and doesn't come back. I've experimented with $jaxl->connect() and $jaxl->startStream(), and $jaxl->startCore() after jaxl_post_disconnect hook, but I think I'm missing something.

3

There are 3 answers

1
Yves Lange On

One solution would be to test your connection:

1) making a "ping" request to your page/controller or whatever
2) setTimeout(functionAjaxPing(), 10000);
3) then read the Ajax response and if == "anyStringKey" then your connection works find
4) else: reconnect() / errorMessage() / whatEver()

This is what IRC chat use i think.

But this will generate more traffic since the ping/ping request will be needed.

Hop this will help you a bit. :)

0
mdpatrick On

What I ended up doing was creating a crontab that simply executed the PHP script again.

In the PHP script I read a specific file for the pid of the last fork. If it exists, the script attempts to kill it. Then the script uses pcntl_fork() to fork the process (which is useful for daemonifying a PHP script anyway) and capture the new PID to a file. The fork then logs in with to Jabber with JAXL per usual.

After talking with the author of JAXL it became apparent this would be the easiest way to go about this, despite being hacky. The author may have worked on this particular flaw in more recent iterations, however.

One flaw to this particular method is it requires pcntl_fork() which is not compiled with PHP by default.

0
Abhinav Singh On

If you are using Jaxl v3.x all you need is to add a callback for on_disconnect event.

Also you must be using XEP-0199 XMPP Ping. What this XEP will do is, periodically send out XMPP pings to connected jabber server. It will also receive server pings and send back required pong packet (for instance if your client is not replying to server pings, jabber.org will drop your connection after some time).

Finally you MUST also use whitespace pings. A whitespace ping is a single space character sent to the server. This is often enough to make NAT devices consider the connection “alive”, and likewise for certain Jabber servers, e.g. Openfire. It may also make the OS detect a lost connection faster—a TCP connection on which no data is sent or received is indistinguishable from a lost connection.