Home » Php » How to develop a web-based XMPP chat facility using PHP and JQuery?

How to develop a web-based XMPP chat facility using PHP and JQuery?

Posted by: admin July 12, 2020 Leave a comment


I am looking to develop a website which features a chat facility between a website visitor and the website administrator.

I know the best way to do this would be using XMPP, however I have no experience using it. I am looking to implement this using PHP.

I’ve downloaded XMPPHP and I edited an example to send a message to my Google Chat client in GMail, but when I reply Google tells me the other end didn’t get the message.

So far, the most informative tutorial is http://www.ibm.com/developerworks/xml/tutorials/x-realtimeXMPPtut/ but I don’t understand why I need to install ‘Openfire’ nor do I want to build the website on my local machine.

Can somebody please tell me what I need (and more importantly, why) to set up this project so I can start to build the code for it?

How to&Answers:

Judging from comments to other answers I am going tell you why, and a little what, but not give you a solution because I see a ton of solutions in the “Related” sidebar. You will have to pick the right one and by knowing “the why” you will be able to make an educated decision.

For chat to feel right, there has to be some immediacy to the responses. A one second lag in time will be noticeable to users over time and give a sense of untimeliness. To make immediate or “real time” responses work in a browser requires a persistent connection so that when new information comes in, it immediately shows up.

Persistent connections in browsers are difficult due to the request/response specifications of HTTP. There are specifications in work to bring persistent connections to browsers but those browsers are not ubiquitous. In the future persistent connections will be supplied by WebSockets and SPDY, both of which are available in the latest versions of Chrome, Safari and FireFox with IE lagging a bit.

Another option for persistent connections is XMPP. XMPP is the protocol used for the Jabber chat client. Since it is an open source implementation it has been ported to many other uses. JavaScript libraries exist that allow you to connect a browser to an XMPP socket and listen for new messages. The method I have seen in the past is to send the messages to the web server, and then have the web server tell the XMPP server about the new message which then broadcasts the new message out to all of the users. However, this requires an XMPP server which raises the complexity of system.

Most users are not on the bleeding edge of browser versions so you will need to be able to handle older browsers. Most of the alternatives involve opening a long running connection to the server which responds whenever new data arrives. Here is a list of methods for simulating a persistent connection in older browsers:

  • Adobe Flash Socket
  • ActiveX HTMLFile (IE)
  • Server-Sent Events (Opera)
  • XHR with multipart encoding
  • XHR with long-polling

These older methods, and WebSockets, are supported by a library called Juggernaut.

UPDATE Juggernaut has been deprecated by the maintainer, for good reason: modern browsers support persistent connections out of the box (with the exception of IE of course) through a specification called Server-Sent Events (SSE). Backwards compatibility is now handled by polyfills (What is a polyfill?) and as the deprecation post notes, there are a couple of good ones to bring SSE to legacy browsers.


There are loads of resources out there which can help you with this. A quick Google search brings up the following:





Instant Messaging apps are supposed to be real time. A website works on HTTP protocol which uses request/response method. One way to do it is POLLING. send a request for new pending messages for the user to the server. The server should be able to differentiate between the messages which has been sent and the ones which are yet to be delivered. this method is called Polling. Your browser is constantly asking the server to send any pending messages.
But this may waste bandwidth and also drain battery ( in case the website is accessed using a smartphone ). Better option is to still use the XMPP server.