Simple online chat room _javascript skills based on JavaScript, Ajax, Memcache and PHP

Source: Internet
Author: User
Tags time interval

On-line chat room (online chatrooms) is a total number of people to communicate the network platform, with more and more new technology inventions and mature use, online chat room also appears to use a variety of different technologies, this article I would like to summarize briefly.

First of all, the front desk technology, the latest is of course based on HTML5 WebSocket, it implements the browser and server Full-duplex communication (Full-duplex). The WebSocket communication protocol was set as standard RFC 6455,websocketapi by the IETF in 2011. In the WebSocket API, the browser and the server only need to do a handshake action, then, between the browser and the server formed a fast channel. The data can be transmitted directly between the two. But because WebSocket is the HTML5 API, many browsers are not supported, so this approach is not very much used now. The second is Ajax, which is a way of polling, every once in a while, go to the server to download data, see whether the update, its advantage is asynchronous request processing (also can set synchronization), will not block other operations of users, the disadvantage is that non-stop polling not only consumes bandwidth, but also takes up too much terminal and server resources, And because Ajax polling has a time interval, the message is not real-time, because it is not a message trigger, Ajax is almost the lowest efficiency. The third type is server-based push technology (comet), said is a push, in fact, is a pseudo push, the principle is in the terminal and the server to establish a long connection, the foreground by determining whether the content of the long connection to get content, it is the advantage of less resources, real time is good, really need server special support, And the connection is occupied for a long time. More and more services are now using this technology.

Again to say backstage, chat room belongs to high IO application, database aspect may be the biggest bottleneck, chat is characterized by short text more, similar to micro Bo. And it belongs to multi-user system, when the user reached an order of magnitude, if the use of MySQL and other relational database, the query pressure should be very large. If there are no special requirements, the individual believes that the chat record does not need permanent storage, this time, Memcache,redis and other memory-based storage systems can play a role. Here's a simple, Ajax+php+memcache-based online chat room I developed.

In this chat room, all chat records are stored in the Memcache, using CID to store the maximum ID of the chat record, with msgcid to store chat records, each inserted a record, CID 1. The foreground obtains the JSON format data via Ajax, because the Memcache is based on the memory characteristic, the overall system operation speed is very fast. The following is the system's source code, the entire system has only two files, together less than 10KB.

Cloud Habitat Community Downloads

Https://github.com/hitoy/online-chatroom

System Requirements:
1, installed with Memcache
2,php installation memcache Extension
3, a modern browser that supports JavaScript and Ajax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.