当前位置:网站首页>How does the server push messages to the client?
How does the server push messages to the client?
2022-08-05 13:32:00 【Front-end watermelon brother】
Hello everyone, I am the front-end watermelon brother. Today, I will show you how the server pushes messages to the client.
Sometimes, we hope that the server can actively push some information to the client.However, the HTTP protocol can only allow the client to initiate a request and the server to respond, but cannot allow the server to actively send information.
To solve this problem, we have three common solutions: short polling, long polling, WebSocket.
Short Polling
Short polling means sending an HTTP request at regular intervals until the required information is obtained.
Assume that the user pays our order with the payment platform, but because the payment platform notifies our server asynchronously, the front-end page cannot get the information on whether the support is successful, and the server needs to inform the front-end.
We use short polling, probably like this:
A: Has the order been completed?
B: No.
A: (After 200ms) Has the order been completed?
B: No.
A: (200ms after receiving the request) Has the order been completed?
B: No.
…(The question and answer continued several times)
A: Has the order been completed?
B: Done.
The polling is over, so A gets the order completion information and displays the order completion UI.
Short polling is simple to implement, and the server does not need to do much special processing, but sending requests frequently will consume more bandwidth.
Long Polling
Long polling: The client sends an HTTP request, The server suspends the connection for a long time, does not return data for a long time, and does not return until the required information is obtained.
It looks like this:
A: Has the order been completed?
…(after a while)
B: Done.
This concludes the polling.
The problem with long polling is that HTTP has become a long connection, and there is no response for a long time, which may be unexpectedly closed due to insufficient resources.In extreme cases, it may also trigger a timeout error of the gateway, returning 504 Gateway timeout.These situations require some strategy to deal with.
In addition, the code implementation is also more complicated than short polling. For example, whether the order is completed, you need to monitor the status change of an order, and then notify a specific request, which is more troublesome than the short polling directly reading the data returned by the database.much more.
WebSocket
Because of the need for the server to actively push messages, the WebSocket protocol appeared.
WebSocket, like the HTTP protocol, is an application layer protocol, and the upper layer also relies on the TCP protocol of the transport layer for data transmission.
The difference is that WebSocket has a complete full-duplex capability, supports active push by the server, and has low overhead.In applications with heavy server push, such as chat rooms, WebSocket is the best choice.
The process of the browser establishing a WebSocket: first send an HTTP request, which includes the relevant header fields that the request is upgraded to WebSocket. If the server supports it, it will return a 101 status code, indicating that the protocol can be switched and a WebSocket connection can be established.
After that, the browser can obtain the active push message from the server by listening to the event.
End
Short rotation training is characterized by simple implementation and does not require much modification work.
Long polling will suspend the HTTP request and return data to the client until the state changes, which can reduce the number of requests and bandwidth, but it needs to consider the situation of unexpected shutdown, and the implementation is complicated.
The WebSocket protocol is a protocol specially invented to solve the problem of service push. It can truly realize the active push of the server and is suitable for scenarios with heavy service push.
I am the front-end watermelon brother, welcome to follow me to learn more front-end interview questions.
边栏推荐
猜你喜欢
随机推荐
比较方法equals( )、==以及CompareTo
【CC3200AI 实验教程2】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-系统测试
地平线初体验.下
R语言patchwork包将多个可视化结果组合起来、plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义分割符号信息(separator
小程序开发商城的因素有哪些?
Mysql索引
pandas连接oracle数据库并拉取表中数据到dataframe中、筛选当前时间(sysdate)到一分钟之前的所有数据(筛选一分钟之内的数据)
Capacity upgrade helps computing power flow, the acceleration moment of China's digital economy
适配器模式
高薪程序员&面试题精讲系列133之微服务里的网关有哪些实现方案?你熟悉Gateway网关吗?
c语言小项目(排雷游戏实现)
《MySQL核心知识》第6章:查询语句
并发刺客(False Sharing)——并发程序的隐藏杀手
What is the relationship between Kalman filter and mean filter?
pyspark实现csv文件转parquet格式(最优解决方案)
链表面试题-刷题
《MySQL核心知识》第4章:简单语法
嵌入式开发:嵌入式软件开发和编程
炒期货去哪开户才是安全的?
可编程直流电源用途广泛可以满足各种直流电源的应用场景









