Recently, PHPChina has launched a special topic on PHP chat room technology. I happen to be working on this project recently, so I would like to express my personal views here.
Main functions of chat room:
1. Register, log in, log out, modify information.
2. The user applies to create a chat room and customizes the chat room attributes.
3. Display the chat room name, description, moderator, announcements, and advertisements.
4. Display the list of online users in real time, and you can view online user information (nickname, email, IP, online time, etc.).
5. Post chat content, font attributes, actions, expressions, and common phrases.
6. Allows invisibility, private chat, scrolling, user blocking, screen clearing, chat scene selection, background music, and sending method customization.
7. Administrators have the right to manage members (kick them out, ban them from speaking, and transfer management rights).
Things to note:
1. It is forbidden to use frame structure (iframe does not bring any benefits except reducing your workload)
2. The front-end and background code are completely separated, data is only transmitted through the ajax channel, and only necessary data should be transmitted. (Display data in the foreground and process data in the background)
3. It is forbidden to refresh the entire page caused by the program, and only update the content that needs to be changed (it is forbidden to use header and meta to refresh the page).
4. When submitting chat content, you cannot blindly pursue the speed of user experience and ignore the actual chat record submission order. (That is to say, the submitted chat records should be reorganized in the actual order on the server and then transmitted to the client, because many people may submit information at the same time)
Technologies used:
1. The bridge between the front and backend-AJAX. You can choose jquery, XAJAX. I prefer jquery, which is fast, simple and durable, has many plug-ins, and is updated quickly.
2. The form of data carrier-XML. In fact, general chat room data is not very complicated, and json is enough.
3. Database abstraction layer-ADODB. It's faster to use PDO. I am already used to using ADODB.
4. The underlying data storage medium - MySQL. There is no doubt about this.
5. Data intermediate storage medium-memcache. Store high-speed read and write data in memcache shared memory to reduce the load on the database.
6. If you want to implement video and voice, you need the support of the server FMS (flash media server), and the client needs to install flash player. And it involves data interaction between html, javascript, flash and the background. There isn't a lot of information on this.
These are the core technologies. In actual application development, there are still many detailed issues that need to be resolved.
For example: How to handle updates to online lists?
There are two ways:
1. Refresh the entire list (whether there are updates or not).
2. Add new online members, delete offline members, and change members whose information has changed.
Obviously the second option is the best option.
How to deal with XmlHttpRequest concurrency issues?
JavaScript is single-threaded. If there are two XmlHttpRequest objects at the same time, problems can easily occur, which requires us to control the generation and end of XmlHttpRequest.
In the chat room, the data we need to update mainly include: online list (including updates of member information), chat records, chat room attributes, etc. The frequency of these updates is inconsistent (if the times are consistent, you only need to create an XmlHttpRequest to handle it). Chat history takes a shorter period of time to update, and online lists can take a little longer to update. Without using a framework, if you create XmlHttpRequest objects separately, concurrency problems are likely to occur. This requires creating a time and process control function.
setInterval('process_control()',3000) //Called once every three seconds
The function process_control implements task scheduling, such as executing tasks at a certain interval and executing the next task only after the task is completed.