Judging by the comments on the other answers, Iβll tell you why and something like that, but I donβt give you a solution, because I see a lot of solutions in the sidebar. You will need to choose the right one, and knowing why, you can make an informed decision.
In order for the chat to feel right, there must be some immediacy in the responses. The time delay will be noticeable to users over time and will give a sense of timeliness. For immediate or "real" answers to work in the browser, a constant connection is required so that when new information appears, it immediately appears.
Persistent connections in browsers are complicated due to the HTTP / HTTP / request specifications. There are specifications in the work to ensure constant connections with browsers, but these browsers are not ubiquitous. In the future, persistent connections will be delivered by WebSockets and SPDY , both of which are available in the latest versions of Chrome, Safari and FireFox with IE slightly behind.
Another option for persistent connections is XMPP . XMPP is the protocol used for the Jabber chat client. Since this is an open source implementation, it has been ported to many other uses. JavaScript libraries exist that allow you to connect your browser to an XMPP socket and listen to new messages. The method I saw in the past is to send messages to the web server, and then inform the web server of the XMPP server about the new message, which then sends the new message to all users. However, this requires an XMPP server, which increases system complexity.
Most users are not on the edge of browser versions, so you will need to handle older browsers. Most alternatives include opening a long connection to a server that responds when new data arrives. The following is a list of persistent connection modeling methods in older browsers:
- Adobe Flash Socket
- ActiveX HTMLFile (IE)
- Server Events (Opera)
- XHR multi-line coding
- Long poll XHR
These old methods and WebSockets are supported by the Juggernaut library.
UPDATE Juggernaut is deprecated by its maintainer for a good reason: modern browsers support persistent connections out of the box (with the exception of IE, of course) through the Server-Sent Events (SSE) specification. Backward compatibility is now handled with polyfills ( What is polyfill? ), And as a postrecation post there are some good ones that bring SSE to legacy browsers.
Sixty4bit
source share