Online chatrooms is an online platform for communication with multiple people. With the invention and maturity of more and more new technologies, online chatrooms have also used various technologies. I would like to briefly summarize this article.
First of all, let’s talk about the front-end technology. The latest one is of course HTML5-based websocket, which implements full-duplex communication between the browser and the server. The WebSocket communication protocol was designated as the standard RFC 6455 by the IETF in 2011, and the WebSocket API was designated as the standard by W3C. In the WebSocket API, the browser and the server only need to shake hands, 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 HTML5 API, many browsers do not yet have support, so this method is not particularly used now. The second type is ajax. It adopts a polling method. Every once in a while, you go to the server to download data and check whether it is updated. Its advantage is asynchronous request processing (synchronization can also be set up) and will not block other operations of the user. The disadvantage is that uninterrupted polling not only consumes bandwidth, but also occupies too much terminal and server resources. Moreover, because there is a time interval for ajax polling, the obtained messages are not real-time, because it is not a message trigger, and ajax's efficiency is almost the lowest. The third type is server-based push technology (comet). It is called push, but it is actually pseudo push. The principle is to establish a long connection between the terminal and the server. The front desk obtains content by determining whether the content of this long connection has changed. Its advantage is that it occupies less resources and has good real-time performance. It does require special support from the server and occupies connection for a long time. Now more and more services adopt this technology.
Let’s talk about the background. Chat rooms are high IO applications, and the database may be the biggest bottleneck. The characteristic of chat is that there are many short texts, similar to Weibo. Moreover, it belongs to a multi-user system. When the user reaches an order of magnitude, if a relational database such as Mysql is used, the query pressure should be very high. If there are no special requirements, I personally believe that chat records do not need to be permanently stored. At this time, memory-based storage systems such as memcache and redis can play a role. 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. The maximum id of chat records is stored with cid and the chat records are stored with msgcid. For each record inserted, the cid will be increased by 1. The front desk obtains json format data through ajax. Since memcache is based on memory, the overall system runs very fast. Below is the source code of the system. There are only two files in the entire system, which are less than 10KB together.
Wulin.com download
https://github.com/hitoy/online-chatroom
System requirements:
1. Memcache is installed
2. Install memcache extension on php
3. Modern browsers that support Javascript and ajax