Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between long polling, WebSocket, server sending events?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what's the difference between long polling, WebSocket and server sending events". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what's the difference between long polling, WebSocket and server sending events" can help you solve the problem.

What's the difference between long polling, WebSocket, and server sending events?

Long polling, WebSocket and server sending events are popular communication protocols between clients and servers, such as Web browsers and Web servers. First, let's look at the standard HTTP Web request. The following is a series of events for regular HTTP requests:

The client opens the connection and requests data from the server.

The server calculates the response.

The server sends the response back to the client based on the open request.

Ajax polling protocol

Polling is a standard technique used by most AJAX applications. The basic idea is that the client repeatedly polls (or requests) the server for data. The client makes a request and waits for the server to respond to the data. If no data is available, an empty response is returned.

The client opens the connection using regular HTTP and requests data from the server.

The requested web page sends the request to the server at regular intervals (for example, 0.5 seconds).

The server calculates the response and sends it back, just like regular HTTP traffic.

The client periodically repeats the above three steps to get updates from the server.

The problem with polling is that the client must constantly ask the server for any new data. Many empty responses will be photographed, resulting in HTTP overhead.

HTTP long polling protocol

This is a variation of traditional polling technology, which allows the server to push information to the client when data is available. With Long-Polling, the client can request information from the server as if it were a normal poll, but the server may not respond immediately. This is why this technique is sometimes called "pending request".

If the server does not have any data available to the client, the server keeps the request and waits until some data is available, rather than sending an empty response.

Once the data is available, the complete response is sent to the client. The client then immediately re-requests information from the server so that the server almost always has a waiting request available, which the server can use to respond to events.

The basic life cycle of an application that uses HTTP Long-Polling is as follows:

The client uses regular HTTP to make the initial request and then waits for a response.

The server delays its response until an update is available or a timeout occurs.

When updates are available, the server sends the complete response to the client.

The client usually sends a new long polling request as soon as it receives the response, or after the allowed wait time is paused.

Each long polling request has a timeout. After the connection is closed due to a timeout, the client must reconnect periodically.

WebSockets protocol

WebSocket provides a communication channel over a full-duplex single TCP connection. It provides a persistent connection between the client and the server that both parties can use to start sending data at any time. The client establishes an WebSocket connection through a process called the WebSocket handshake. If the process is successful, the server and client can exchange data in both directions at any time. WebSocket protocol makes the communication between the client and the server have a lower overhead, thus promoting the real-time data transmission with the server. By providing a standardized method, the server sends content to the browser without client inquiry, and allows messages to be passed back and forth while keeping the connection open.

Server send event Protocol (SSEs)

Under SSEs, the client establishes a long-term connection with the server. The server uses this connection to send data to the client. If the client wants to send data to the server, it needs to use another technology / protocol.

The client uses regular HTTP to request data from the server.

The requested web page will open a connection to the server.

Whenever new information is available, the server sends the data to the client.

SSEs is best when we need real-time traffic from the server to the client, or when the server is cycling to generate data and will send multiple events to the client.

Summary

Common front and back end message communication protocols are as follows:

Traditional polling protocol

Http long polling protocol

WebSockets protocol

SSEs protocol

Analysis of SSEs Protocol

In addition to the SSEs protocol, other methods are familiar to everyone. Let's take a look at what the SSEs is.

SSE, namely Server-Sent Events, also known as EventSource, is a server-side event push technology that has been written into the HTML 5 standard. It allows the establishment of an one-way channel between the client and the server to allow the server to continuously push event messages to the client in a single direction. SSE is suitable for scenarios that do not need to send data from the client, but need to be updated through some server operations. For example, stock quotation, sharing facility update, friend status update and so on.

The general process has been briefly introduced above. To put it simply, the so-called SSE is that the browser sends a HTTP request to the server, and then the server continuously pushes "message" to the browser in one direction. The format of this message is simple: "message" is prefixed with "data:" and ends with "\ n\ n".

SSE and WebSocket have similar functions, both are used to establish communication channels between browsers and servers.

The comparison between the two is as follows:

SSEWebsockethttp protocol, which can run directly on the existing proxy server and independent websocket protocol of authentication technology, requires the server side to support that SSE is an one-way channel and can only be sent to the browser side by the server. Full-duplex channel, can communicate two-way, more powerful and lightweight, relatively complex to implement relatively complex default support disconnection reconnection requires self-implementation of heartbeat reconnection text transmission binary transmission support custom message type none

Client DEMO

If (!! window.EventSource) {var source = new EventSource ('push'); / / Open connection source.addEventListener (' open', function (evt) {/ / console.info ("connection is already open");}, false); / / receive message source.addEventListener ('message', function (evt) {console.info (evt.data)) }); / / error message, and close notification source.addEventListener ('error', function (evt) {/ / console.info (evt);}, false);} else {alert ("browser does not support SSE")}

Server DEMO

First declare to the client that the next thing to send is data of type text/event-stream of the event stream, and then you can send messages to the client multiple times. An event stream is a simple text stream that only supports encoding in UTF-8 format. Each message is delimited by a blank line.

@ RestController public class SSEsController {/ / send data @ RequestMapping (value = "/ push", produces = "text/event-stream") public String push () throws InterruptedException {String msg =...; / / fixed format return "data:" + msg + "\ n\ n" to the browser }} this is the end of the content about "what's the difference between long polling, WebSocket, and server sending events". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report