Which technology do you choose to implement the web chat room?
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the function is very simple. Currently, it can only send text messages and picture messages. The interface is as follows
To implement the chat function on the web page, there are currently two ways to choose from. One is to use ajax-long polling, and the other is to use websocket technology in html5.
1. Ajax-long polling
Before HTML5 appeared, I thought that this method would be the way to implement the chat function on the web page! The so-called long polling is nothing more than real-time request for data from the server, that is, to pull data from the server. This requires using js to set a timer setinterval() in the web page to request the server once every certain period of time. If there is new data, the client will display this data on its own page. The whole process can be represented by the following figure
Using this method requires an infinite loop request until you leave the chat page, which consumes a lot of resources. However, it was a good method before HTML appeared.
2. HTML5 WebSocket
With the rise of HTML5, WebSocket is gradually becoming known to people. Using WebSocket to implement web chat will replace Ajax, and this chat method is becoming more and more widely used.
Websocket protocol provides full-duplex communication, which is different from HTTP protocol. In addition, Websocket protocol is established on a single TCP link, and its transmitted information flow is based on TCP. Websocket protocol defines ws or wss as URI to encrypt or decrypt links. For example, we can use ws://127.0.0.1 address as the link address of websocket service. Currently, websocket protocol has been supported by browsers such as Google, IE, Firefox, Safari and Opera.
The process of Websocket chat is as follows
In this way, both the client and the server can push messages to each other. When there is no new message, everyone is listening to the corresponding port on their own end. It can be considered that each browser and the server have established a data channel, and everyone is listening at their own channel port (of course, including the server). When a browser pushes a message to the server, the server receives the message and then pushes the message to all browsers connected to it (including the browser that actively pushes messages to the server). Such a process can realize the web version of chat.
Of course, not all browsers support the websocket protocol now, so in order to achieve wider compatibility, Guillermo Rauch created the websocket API - socket.io.
Socket.io is a JavaScript library, consisting of two parts: one is the client library; the other is the server library, which is implemented using NodeJS. These two parts use almost the same API, and socket.io is an event-driven mechanism like NodeJS.
The above are two ways to implement web chat at present. We can choose any one of them according to the actual situation of the project. Of course, it is recommended to use websocket. This article only introduces two ways to implement web chat, and does not involve any implementation code. In the next article, I will introduce how to use socket.io to build a simple chat room. If you have any good suggestions, please leave a message below to discuss and improve together.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u
Adding network mode in Docker Compose
Publish Date:2025/03/25 Views:138 Category:Docker
-
By default, a single network is created by Docker Compose in our application and each container is added there as a service. Every container on the network can be accessed and found by containers on the single network. We can configure our
Using Docker network host commands
Publish Date:2025/03/24 Views:51 Category:Docker
-
Docker containers work by leveraging network drivers that are created during the installation of Docker. The default drivers available to us include bridge and host networking. When we create containers without specifying a network, they ar
Mapping a network drive in a batch script
Publish Date:2025/03/21 Views:72 Category:OPERATING SYSTEM
-
This article will discuss how to map a network drive in a batch script. Mapping a network drive in a batch script For this purpose, we will see three formats of the same command. However, the general format of the command is: net use P: "\\
What multipart/form-data does in post Upload upload files
Publish Date:2025/03/18 Views:64 Category:NETWORK
-
Everyone has used the attribute enctype="multipart/form-data" when uploading files using a form. What is the role of multipart/form-data? Let's talk about this topic. First, let's look at a case Look at the first code form action= "handl
About application/x-www-form-urlencoded
Publish Date:2025/03/18 Views:148 Category:NETWORK
-
As a data format of form, application/x-www-form-urlencoded has its own characteristics form action= "handle.php" method= "post" input type= "text" name= " uname" class= " uname" /br / input type= "text" name= "email" class=
My understanding of webservice is this
Publish Date:2025/03/18 Views:150 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
WSDL looks like this
Publish Date:2025/03/18 Views:192 Category:NETWORK
-
When I first started learning Webservice, I found that there were quite a lot of knowledge points involved, and each point could be a school of its own. Especially when I saw WSDL, I looked up information for a long time, but I was still a