PHP Jabber: if I login and check messages and disconnect, on the other users end I will show up as disconnected

StackOverflow https://stackoverflow.com/questions/847854

  •  21-08-2019
  •  | 
  •  

Question

Am not sure if what I am doing is absolutely correct. But here goes:

  1. User logins into chat via web-based interface
  2. User is informed of updates via Comet
  3. User enters details which goto a PHP file which further connects to a Jabber server

Now the problem is that when the user wants to send a message, it's simple, run php in which i connect to jabber server and send the message. The problem arises when I am waiting for a message. Cause if I login and check messages and disconnect, on the other users end I will show up as disconnected.

Am I approaching this problem in a wrong way? Should I directly connect to the Jabber server (via javascript) instead of a PHP layer in between? How to recieve messages via PHP?

Was it helpful?

Solution

this is an inherent problem (or feature) with http - there are no lasting connections (not really). you need a workaround, there is no real solution.

you could do it with java or flash, but that's not really nice (javascript 4tw!).

the other possibility would be to create an intermediate client what translates connections between the browser and the webserver to connections between the webserver and the jabber server. messy, but possible.

or maybe there is an API that helps with this.

directly connecting to the jabber server via javascript
i possibly slept through the latest ajax-inventions, but afaik you can only communicate with the host the source-html file comes from (ignoring greasmonkey and addons). no different domains, no different ports, period. unless you're going to teach your jabber server how to serve your chatpage-html to the browser, this will get problematic. moreover, staying connected doesn't even work, because that would require multipart-responses. those are only supported by mozilla, and this is why the ugly duckling COMET even exists in the first place. comet itself is a workaround to avoid the inability to hold connections while transfering data.

OTHER TIPS

I haven't tried it out, but you might want to look at xmpphp. Secondly, you might want to consider keeping the user logged in to the XMPP server (aka a Jabber server) for as long as they're logged in to your website. You probably want to have a timeout of some kind in case they leave your website and don't come back.

As for whether or not you should connect via JavaScript, I don't see why you couldn't. I would suggest that you go for whatever seems the simplest to you. You might want to check out Strophe, which I hear good things about, for that case.

The only XMPP library that I've used extensively though is headstock, but that requires using python and Kamaelia.

So the issue, as far as I can tell, is that when the Jabber user on the other end responds. The problem there, at least in part, is that the user is responding to another user on the Jabber server, yet you want the php script to be aware that this response has taken place without holding the connection open (which makes sense since the script is no longer running, probably).

One option, albeit a really silly one, is:

  • Have a php script that can broker a connection to the Jabber server for both sending and receiving for the user on your page,

  • Use AJAX to send messages for the user (the AJAX would point to the above script, the script would send the message.)

  • Have a Javascript infinite loop that pings the same script ever 10 seconds or so, checking in to see if there are messages. If there are, they get passed back to the client and output to the user.

There are only two issues with the above:

1) If the user isn't connected when the message is transmitted, will the php script still see/get the message?

2) A client side loop that makes ajax requests every 3 seconds would probably be a huge drain.

Solution 2:

OpenFire jabber server. It comes with a web chat client built in, and it has an addon called Fastpath, which is meant to handle HTML-based chats on the client end (like the "chat with an agent now!" feature on too many support pages.)

We use this at work and it is very customizable, can be integrated with other scripts (for instance, if you want a script that fills in the user details from their login, or adds some custom avatar, or whatever), and it (OpenFire) has tons of other extensions and addons that, if this isn't what you want, they probably have what you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top