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

↘锁芯ラ 提交于 2019-12-03 00:22:19

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!