Question: It is to make a function similar to the QQ chat interface on the homepage of the website. It does not need to be complicated. It only needs to display the user's avatar and the content sent. There is no need to record the chat. Just display the latest 20 records in real time?
Personal idea: Since the function is relatively simple and not very complicated, and the displayed content only needs the latest 20 items, I plan to use the AJAX polling method to do it. When the user enters the chat record, the record will be saved. Database (AJAX polling to read database data in real time), and then after obtaining the data, display it on the page. Is it okay to do this? Will there be any problems when chatting with multiple people? For example, when data is displayed on the page, it will be very slow or something?
Websoket can be used to do this, but I have never used it, and I don’t know if it can be used on a windows server (the project is deployed on windows).
Here comes the question: Do you have any good and simple methods? Give me some advice! ! ! !
为情所困2017-06-28 09:24:36
websocket
or socket.io
can be achieved. This has less to do with the operating platform and more to do with the web service program you implement, because you have to choose one web program to support websocket
or socket .io. I have implemented it with
node.js` before, and I also refer to the examples on the Internet. You can also refer to
Node.js + Web Socket to create an instant chat program HiChat
高洛峰2017-06-28 09:24:36
With AJAX polling, when multiple people are chatting, each terminal has to continuously request database data, which has the following disadvantages:
Due to the large number of requests, it will increase the pressure on the server.
AJAX polling itself is slow
Since browsers have limits on the number of AJAX requests under the same domain name, the efficiency of data interaction is still quite low.
If you use WebSocket, I recommend using ws
of nodeJS
to make a broadcast service. It can be hung in the Windows background or transplanted at will. It is quite convenient. The library used is this: https://www.npmjs .com/package/ws
I have to say that the things written in NodeJS
are quite good in terms of portability, scalability and compatibility.
I won’t go into the specific operation. You can refer to the broadcase example
example of the ws
package to write it.