tags:

views:

642

answers:

3

Hello,

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, its 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?

Thank you for your time.

+1  A: 

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.

Jason Baker
I was planning to use XMPPHP. But how do i keep them logged in for the whole time? Am lost. What about http-binding?
Alec Smart
I wanted to use PHP in between because I wanted to log and process the data before sending it to the other user.
Alec Smart
Also how do I get the data? I believe that PHP will have to be awake when a request comes. But obviously I will be periodically checking using PHP, so I will end up missing messages.Am trying to use OpenFire, somehow I need it to call a php file everytime a message arrives. That would be best? Is that possible?
Alec Smart
imho you need a php client (using xmpphp) that puts all jabber traffic in a db. requests from web clients then query the database, not the server directly.
Schnalle
@schnalle, I believe the same, but how do I keep the php client continuously listening for messages? I mean do I run it as a service for each and every user? Or how do I run a single instance of php which records all the users input/output?
Alec Smart
A: 

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.

Schnalle
I have comet, jabber, apache server ready. The damn problem is how to link all of them? i.e. how to listen to messages using xmpphp and put it in the database everytime a message comes/send the message using comet to the user? Am confused.
Alec Smart
i'm not really familiar with the xmpphp package. the only way i could think of to get all that working is a bit complex. in fact i think you'd need a separate client running (not an apache script, but a cli/shellscript) that communicates with the jabber server via xmpphp, logs everything to the database and reads database for messages to push to the jabber-server, all via sockets (xmpphp), emulating multiple jabber-users. different php/javascript/comet handle communication between client and database. not sure if that's the only/best way, but i can't think of anything else.
Schnalle
A: 

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.

Anthony