Home > Web Front-end > JS Tutorial > Simple online chat room based on javascript, ajax, memcache and PHP_javascript skills

Simple online chat room based on javascript, ajax, memcache and PHP_javascript skills

WBOY
Release: 2016-05-16 16:16:02
Original
1419 people have browsed it

Online chatrooms are a network platform for multiple people to communicate. With the invention and mature application of more and more new technologies, online chatrooms have also appeared using various technologies. This I would like to briefly summarize this article.

First of all, let’s talk about front-end technology. The latest one is of course websocket based on HTML5, which realizes full-duplex communication between the browser and the server. The WebSocket communication protocol was set as a standard RFC 6455 by the IETF in 2011, and the WebSocket API was set as a standard by the W3C. In the WebSocket API, the browser and the server only need to perform a handshake action, and then a fast channel is formed between the browser and the server. Data can be transmitted directly between the two. However, since Websocket is an API of HTML5 and is not supported by many browsers, this method is not particularly used now. The second is ajax, which uses a polling method. Every once in a while, it downloads data from the server to check whether it has been updated. Its advantage is asynchronous request processing (synchronization can also be set), which does not block other users. Operation, the disadvantage is that uninterrupted polling not only consumes bandwidth, but also takes up too many terminal and server resources, and because ajax polling has a time interval, the information obtained is not real-time, because it is not triggered by a message, and the efficiency of ajax is almost is the lowest. The third type is server-based push technology (comet). It is said to be push, but in fact it is pseudo-push. The principle is to establish a long connection between the terminal and the server. The front desk obtains the content by determining whether the content of this long connection has changed. Its advantages are that it takes up less resources and has good real-time performance. It does require special support from the server and takes up the connection for a long time. More and more services now use this technology.

Let’s talk about the backend. Chat rooms are high-IO applications, and the database may be the biggest bottleneck. Chat is characterized by a lot of short texts, similar to Weibo. And it is a multi-user system. When the number of users reaches an order of magnitude, if a relational database such as Mysql is used, the query pressure should be great. If there are no special requirements, I personally think that chat records do not need to be permanently stored. At this time, memory-based storage systems such as memcache and redis can come into play. Below is a simple online chat room based on ajax php memcache that I developed.

In this chat room, all chat records are stored in memcache. cid is used to store the maximum id of the chat records, and msgcid is used to store the chat records. Each time a record is inserted, cid increases by 1. The front desk obtains json format data through ajax. Since memcache is memory-based, the overall system runs very fast. Below is the source code of the system. There are only two files in the entire system, which together are less than 10KB.

Script Home Download

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

System requirements:
1. Memcache is installed
2. PHP install memcache extension
3. Modern browsers that support Javascript and ajax

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template