Data pushing method, system and equipment based on SSE server and medium

Through the data push method of the SSE server, the long connection feature of the HTTP protocol is used to solve the resource waste problem of WebSocket in one-way communication scenarios, and realize lightweight and efficient real-time data communication, which is suitable for applications that require real-time data updates. Scene.

CN119276931AInactive Publication Date: 2025-01-07INSPUR TIANYUAN COMM INFORMATION SYST CO LTD

Patent Information

Application Number
CN202411408175.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-10
Publication Date
2025-01-07
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

WebSocket consumes a lot of resources in one-way communication scenarios, which seems too complex to effectively solve the stateless and one-way communication problems of traditional HTTP protocols.

Method used

Using the server-Sent Events (SSE) data push method, responding to the client's GET request through the server side, setting the response header Content-Type to text/event-stream, Cache-Control to no-cache, and Connection to keep-alive. Establish a persistent HTTP connection. When data is updated, the server sends an event stream to the client through a persistent HTTP connection. The client uses a fetch or EventSource object to listen and process the received event stream.

Benefits of technology

It realizes lightweight and efficient real-time data communication, which is more efficient than WebSocket in one-way communication scenarios, reduces unnecessary resource consumption, and is simple to implement, and is suitable for application scenarios that require real-time data updates, such as stock price Updates and real-time news push.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119276931A_ABST
    Figure CN119276931A_ABST
Patent Text Reader

Abstract

The invention discloses a data pushing method, system and device based on an SSE server side and a medium, belongs to the technical field of network communication, aims to solve the technical problem of resource waste of WebSocket in a one-way communication scene in the prior art, and adopts the technical scheme that the server side responds to a GET request of a client side, sets a response header Content-Type as text / event-stream, sets a Cache-Control as no-cache and sets Consection as kep-alive, and sends the GET request to the client side; a persistent HTTP connection is established; when the data is updated, the server side sends an event stream to the client side through the persistent HTTP connection; and the client monitors and processes the received event stream by using a feed or EventSource object.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of network communication technology, and in particular to a data push method, system, device and medium based on an SSE server. Background Art

[0002] WebSocket is a network communication protocol built on top of the TCP protocol. It provides two-way communication capabilities and effectively solves the stateless and one-way communication problems of the HTTP protocol. The features of WebSocket are as follows:

[0003] ① Two-way communication: WebSocket implements two-way communication between the server and the client, that is, the server can actively push information to the client, and the client can also actively send information to the server.

[0004] ②Persistent connection: Once a WebSocket connection is established, it will persist until it is explicitly closed. This persistent connection avoids the overhead of frequent handshakes and disconnections in the HTTP request / response model.

[0005] ③ Efficiency: WebSocket uses less overhead to maintain the connection, and the data format is lightweight, the performance overhead is small, and the communication is efficient.

[0006] ④Compatibility and flexibility: WebSocket has good compatibility with HTTP protocol, the default ports are also 80 and 443, and the handshake phase uses HTTP protocol. In addition, WebSocket can send text and binary data, and there is no same-origin restriction.

[0007] HTTP protocol, full name Hyper Text Transfer Protocol, is a transmission protocol used to transfer hypertext from the World Wide Web (WWW) server to the local browser. HTTP is a protocol based on the TCP / IP communication protocol to transfer data. It belongs to the application layer protocol and is an object-oriented protocol. The HTTP protocol works on the client-server architecture. The browser, as an HTTP client, sends all requests to the HTTP server (i.e., the WEB server) through the URL. After receiving the request, the Web server sends a response message to the client.

[0008] The traditional HTTP protocol is a request-response model, which makes it impossible for the server to actively push data to the client. Although WebSocket provides two-way communication capabilities, it is too complex and consumes a lot of resources in some scenarios where only one-way communication is required. Summary of the invention

[0009] The technical task of the present invention is to provide a data push method, system, device and medium based on an SSE server to solve the problem of resource waste in the prior art of WebSocket in a one-way communication scenario.

[0010] The technical task of the present invention is achieved in the following way: a data push method based on an SSE server, the method is as follows:

[0011] The server responds to the client's GET request, sets the response header Content-Type to text / event-stream, Cache-Control to no-cache, and Connection to keep-alive, and establishes a persistent HTTP connection.

[0012] When data is updated, the server sends an event stream to the client through a persistent HTTP connection;

[0013] The client uses fetch or EventSource objects to listen to and process the received event stream.

[0014] Preferably, the event stream includes event type, event ID, data content, and optional retry time;

[0015] Different events are separated by a blank line containing only carriage return and line feed characters (\r\n\r\n).

[0016] Preferably, the server records and sends the ID of each event, and when the client reconnects after a connection interruption, it sends the most recently received event ID through the HTTP header Last-Event-ID to achieve data synchronization.

[0017] Preferably, the client uses fetch or EventSource objects to listen to and process the received event stream as follows:

[0018] Initiate a request: The client sends a GET request with a specified header to the server, indicating the type of event stream to receive and disabling any event cache;

[0019] Receiving data: The client uses JavaScript's EventSource object to listen to the event stream sent by the server; when a new event arrives, the EventSource object triggers the corresponding event processing function according to the event type;

[0020] Error handling and reconnection: The client handles connection errors by listening to the onerror event and automatically reconnects when necessary.

[0021] Preferably, the server side implements SSE in accordance with the following specifications:

[0022] Set SSE-related response headers (event stream, long connection, chunk transfer - event stream comes with and cache is disabled); the situation is as follows:

[0023] Set the response header res.setHeader("Content-Type","text / event-stream;char set=utf-8"), the server-side response data type is the event stream of text / event-stream;

[0024] Set the response header res.setHeader("Connection","keep-alive") to establish a keep-alive long connection between the client and the server; the response content will be divided into multiple chunks for transmission;

[0025] Encapsulate data into events and send them to the client in a set format;

[0026] Set the appropriate delay and buffer to control the sending timing (applicable to dynamically generated content and large data transmission);

[0027] Disconnect clients at a set time.

[0028] Preferably, the client uses the fetch method to call the service's interface and / or see as follows:

[0029] Get the ReadableStream object res.body;

[0030] Get a reader object res.body.getReader();

[0031] Through the reader.read() method, asynchronously read the content of the response body (ReadableStreamDefaultReader);

[0032] Convert the data stream to a UTF-8 string;

[0033] Split field data based on \n according to the event stream specification;

[0034] Continue to read the data stream and wait for the server to disconnect or close manually.

[0035] A data push system based on an SSE server includes a server and a client. The server responds to a GET request from a client, sets the response header Content-Type to text / event-stream, Cache-Control to no-cache, and Connection to keep-alive, and establishes a persistent HTTP connection. When data is updated, the server sends an event stream to the client through a persistent HTTP connection. The client uses a fetch or EventSource object to listen to and process the received event stream.

[0036] Preferably, the server side implements SSE in accordance with the following specifications:

[0037] Set SSE-related response headers (event stream, long connection, chunk transfer - event stream comes with and cache is disabled); the situation is as follows:

[0038] Set the response header res.setHeader("Content-Type","text / event-stream;char set=utf-8"), the server-side response data type is the event stream of text / event-stream;

[0039] Set the response header res.setHeader("Connection","keep-alive") to establish a keep-alive long connection between the client and the server; the response content will be divided into multiple chunks for transmission;

[0040] Encapsulate data into events and send them to the client in a set format;

[0041] Set the appropriate delay and buffer to control the sending timing (applicable to dynamically generated content and large data transmission);

[0042] Disconnect clients at a set time;

[0043] The client uses the fetch or EventSource object to listen to and process the received event stream as follows:

[0044] Initiate a request: The client sends a GET request with a specified header to the server, indicating the type of event stream to receive and disabling any event cache;

[0045] Receiving data: The client uses JavaScript's EventSource object to listen to the event stream sent by the server; when a new event arrives, the EventSource object triggers the corresponding event processing function according to the event type;

[0046] Error handling and reconnection: The client handles connection errors by listening to the onerror event and automatically reconnects when necessary;

[0047] The client uses the fetch method to call the service's interface and / or see, as follows:

[0048] Get the ReadableStream object res.body;

[0049] Get a reader object res.body.getReader();

[0050] Through the reader.read() method, asynchronously read the content of the response body (ReadableStreamDefaultReader);

[0051] Convert the data stream to a UTF-8 string;

[0052] Split field data based on \n according to the event stream specification;

[0053] Continue to read the data stream and wait for the server to disconnect or close manually.

[0054] Among them, Server-Sent Events (SSE), as a lightweight alternative, uses the long connection feature of the HTTP protocol to realize real-time data push from the server to the client. It has the advantages of simple implementation, low resource consumption, and good compatibility.

[0055] An electronic device comprising: a memory and at least one processor;

[0056] Wherein, the memory stores a computer program;

[0057] The at least one processor executes the computer program stored in the memory, so that the at least one processor executes the data push method based on the SSE server as described above.

[0058] A computer-readable storage medium stores a computer program, and the computer program can be executed by a processor to implement the above-mentioned data push method based on an SSE server.

[0059] The data push method, system, device and medium based on the SSE server of the present invention have the following advantages:

[0060] (I) The present invention aims to provide a lightweight and efficient real-time data communication solution. SSE, as a server push technology based on HTTP 1.1, establishes a persistent connection through a GET request, allowing the server to actively push data to the client. It is lightweight, highly portable, and easy to use. It is suitable for application scenarios that require real-time data updates, such as stock price updates, real-time news push, etc.

[0061] (II) The present invention realizes efficient and real-time data push from the server to the client through SSE technology, which has the following advantages:

[0062] ① Efficiency: Compared with WebSocket, SSE is more efficient in one-way communication scenarios, reducing unnecessary resource consumption;

[0063] ②Simplicity: simple implementation, small amount of client and server code, easy to deploy and maintain;

[0064] ③Good compatibility: Based on HTTP protocol, compatible with most modern browsers, no additional configuration or plug-ins required. BRIEF DESCRIPTION OF THE DRAWINGS

[0065] The present invention is further described below in conjunction with the accompanying drawings.

[0066] Attached Figure 1 This is a schematic diagram of the data push method based on the SSE server. DETAILED DESCRIPTION

[0067] The data push method, system, device and medium based on the SSE server of the present invention are described in detail below with reference to the accompanying drawings and specific embodiments of the specification.

[0068] Embodiment 1:

[0069] As attached Figure 1 As shown, this embodiment provides a data push method based on the SSE server, and the method is specifically as follows:

[0070] S1. The server responds to the client's GET request, sets the response header Content-Type to text / event-stream, Cache-Control to no-cache, and Connection to keep-alive, and establishes a persistent HTTP connection.

[0071] S2: When data is updated, the server sends an event stream to the client through a persistent HTTP connection;

[0072] S3. The client uses the fetch or EventSource object to listen to and process the received event stream.

[0073] The event stream in step S2 of this embodiment includes event type, event ID, data content, and optional retry time;

[0074] Different events are separated by a blank line containing only carriage return and line feed characters (\r\n\r\n).

[0075] The server in this embodiment records and sends the ID of each event. When the client reconnects after a connection interruption, it sends the most recently received event ID through the HTTP header Last-Event-ID to achieve data synchronization.

[0076] In step S3 of this embodiment, the client uses the fetch or EventSource object to monitor and process the received event stream as follows:

[0077] S301, initiate a request: the client sends a GET request with a specified header to the server, indicating the type of event stream to be received and disabling any event cache;

[0078] S302, receiving data: the client uses JavaScript's EventSource object to monitor the event stream sent by the server; when a new event arrives, the EventSource object triggers the corresponding event processing function according to the event type;

[0079] S303, error handling and reconnection: The client handles connection errors by listening to the onerror event and automatically reconnects when necessary.

[0080] The server-side implementation of SSE in this embodiment complies with the following specifications:

[0081] (1) Set SSE-related response headers (event stream, long connection, chunk transfer - event stream built-in and cache disabled); the situation is as follows:

[0082] ① Set the response header res.setHeader("Content-Type","text / event-stream;charset=utf-8"), the server-side response data type is the event stream of text / event-stream;

[0083] ②Set the response header res.setHeader("Connection","keep-alive") to establish a keep-alive long connection between the client and the server; the response content will be divided into multiple chunks for transmission;

[0084] (2) Encapsulate the data into an event and send it to the client in a set format;

[0085] (3) Setting the corresponding delay and buffer control sending timing (applicable to dynamically generated content and large data transmission);

[0086] (4) Disconnect the client at the set time.

[0087] The sample code is as follows:

[0088]

[0089] It is a technology used to push data from the server to the client in real time. It is based on the HTTP protocol and uses a long connection to maintain communication between the server and the client. Compared with traditional polling or WebSocket-based real-time communication, SSE is simple to use and lightweight. ";

[0090]

[0091]

[0092] console.log(`Example app listening on port${port}`);

[0093] }).

[0094] The client in this embodiment uses the fetch method to call the service interface and / or see, as follows:

[0095] (1) Get the ReadableStream object res.body;

[0096] (2) Get a reader object res.body.getReader();

[0097] (3) Asynchronously read the content of the response body through the reader.read() method (ReadableStreamDefaultReader);

[0098] (4) Convert the data stream into a UTF-8 string;

[0099] (5) Split the field data based on \n according to the event stream specification;

[0100] (6) Continue to read the data stream and wait for the server to disconnect or close manually.

[0101] The sample code is as follows:

[0102]

[0103]

[0104]

[0105] Embodiment 2:

[0106] This embodiment provides a data push system based on an SSE server, which includes a server and a client. The server responds to a GET request from the client, sets the response header Content-Type to text / event-stream, Cache-Control to no-cache, and Connection to keep-alive, and establishes a persistent HTTP connection. When data is updated, the server sends an event stream to the client through a persistent HTTP connection. The client uses a fetch or EventSource object to listen to and process the received event stream.

[0107] The server-side implementation of SSE in this embodiment complies with the following specifications:

[0108] (1) Set SSE-related response headers (event stream, long connection, chunk transfer - event stream built-in and cache disabled); the situation is as follows:

[0109] ① Set the response header res.setHeader("Content-Type","text / event-stream;charset=utf-8"), the server-side response data type is the event stream of text / event-stream;

[0110] ②Set the response header res.setHeader("Connection","keep-alive") to establish a keep-alive long connection between the client and the server; the response content will be divided into multiple chunks for transmission;

[0111] (2) Encapsulate the data into an event and send it to the client in a set format;

[0112] (3) Setting the corresponding delay and buffer control sending timing (applicable to dynamically generated content and large data transmission);

[0113] (4) Disconnect the client at the set time.

[0114] In this embodiment, the client uses the fetch or EventSource object to monitor and process the received event stream as follows:

[0115] (1) Initiate a request: The client sends a GET request with a specified header to the server, indicating the type of event stream to receive and disabling any event cache;

[0116] (2) Receiving data: The client uses JavaScript's EventSource object to listen to the event stream sent by the server; when a new event arrives, the EventSource object triggers the corresponding event processing function based on the event type;

[0117] (3) Error handling and reconnection: The client handles connection errors by listening to the onerror event and automatically reconnects when necessary.

[0118] The client in this embodiment uses the fetch method to call the service interface and / or see, as follows:

[0119] (1) Get the ReadableStream object res.body;

[0120] (2) Get a reader object res.body.getReader();

[0121] (3) Asynchronously read the content of the response body through the reader.read() method (ReadableStreamDefaultReader);

[0122] (4) Convert the data stream into a UTF-8 string;

[0123] (5) Split the field data based on \n according to the event stream specification;

[0124] (6) Continue to read the data stream and wait for the server to disconnect or close manually.

[0125] Embodiment 3:

[0126] This embodiment also provides an electronic device, including: a memory and a processor;

[0127] Wherein, the memory stores computer-executable instructions;

[0128] The processor executes the computer-executable instructions stored in the memory, so that the processor executes the data push method based on the SSE server in any embodiment of the present invention.

[0129] The processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The processor may be a microprocessor or any conventional processor, etc.

[0130] The memory can be used to store computer programs and / or modules. The processor realizes various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, at least one application required for a function, etc.; the data storage area can store data created according to the use of the terminal, etc. In addition, the memory can also include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (SMC), a secure digital (SD) card, a flash memory card, at least one disk storage period, a flash memory device, or other volatile solid-state storage devices.

[0131] Embodiment 4:

[0132] This embodiment also provides a computer-readable storage medium, which stores a plurality of instructions, which are loaded by a processor, so that the processor executes the data push method based on the SSE server in any embodiment of the present invention. Specifically, a system or device equipped with a storage medium can be provided, on which a software program code that implements the functions of any of the above embodiments is stored, and a computer (or CPU or MPU) of the system or device reads and executes the program code stored in the storage medium.

[0133] In this case, the program code itself read from the storage medium can realize the function of any one of the above-mentioned embodiments, and thus the program code and the storage medium storing the program code constitute a part of the present invention.

[0134] The storage medium embodiments for providing the program code include a floppy disk, a hard disk, a magneto-optical disk, an optical disk (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RYM, DVD-RW, DVD+RW), a magnetic tape, a non-volatile memory card, and a ROM. Alternatively, the program code can be downloaded from a server computer via a communication network.

[0135] In addition, it should be clear that the functions of any of the above embodiments can be implemented not only by executing the program code read by the computer, but also by enabling an operating system operating on the computer to complete part or all of the actual operations based on instructions from the program code.

[0136] In addition, it can be understood that the program code read from the storage medium is written to a memory provided in an expansion board inserted into the computer or written to a memory provided in an expansion unit connected to the computer, and then based on the instructions of the program code, a CPU installed on the expansion board or the expansion unit is enabled to perform part or all of the actual operations, thereby realizing the functions of any of the above-mentioned embodiments.

[0137] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A data push method based on an SSE server, characterized in that: The method is as follows: The server responds to the client's GET request, sets the response header Content-Type to text / event-stream, Cache-Control to no-cache, and Connection to keep-alive, and establishes a persistent HTTP connection. When data is updated, the server sends an event stream to the client through a persistent HTTP connection; The client uses fetch or EventSource objects to listen to and process the received event stream.

2. The data push method based on the SSE server according to claim 1, characterized in that: The event stream includes event type, event ID, data content, and optional retry time; Different events are separated by a blank line containing only carriage return and line feed characters.

3. The data push method based on the SSE server according to claim 1 is characterized in that: The server records and sends the ID of each event. When the client reconnects after a connection interruption, it sends the most recently received event ID through the HTTP header Last-Event-ID to achieve data synchronization.

4. The data push method based on the SSE server according to claim 1 is characterized in that: The client uses the fetch or EventSource object to listen to and process the received event stream as follows: Initiate a request: The client sends a GET request with a specified header to the server, indicating the type of event stream to receive and disabling any event cache; Receiving data: The client uses JavaScript's EventSource object to listen to the event stream sent by the server; when a new event arrives, the EventSource object triggers the corresponding event processing function according to the event type; Error handling and reconnection: The client handles connection errors by listening to the onerror event and automatically reconnects when necessary.

5. The data push method based on the SSE server according to claim 1, characterized in that: The server-side implementation of SSE complies with the following specifications: Set SSE-related response headers (event stream, long connection, chunk transfer - event stream comes with and cache is disabled); the situation is as follows: Set the response header res.setHeader("Content-Type","text / event-stream;char set=utf-8"), the server-side response data type is the event stream of text / event-stream; Set the response header res.setHeader("Connection","keep-alive") to establish a keep-alive long connection between the client and the server; the response content will be divided into multiple blocks for transmission; Encapsulate data into events and send them to the client in the set format; Set the corresponding delay and buffer to control the sending timing; Disconnect clients at a set time.

6. The data push method based on the SSE server according to any one of claims 1 to 5, characterized in that: The client uses the fetch method to call the service's interface and / or see as follows: Get the readable stream object res.body; Get a reader object res.body.getReader(); Through the reader.read() method, the content of the response body is read asynchronously; Convert the data stream to a UTF-8 string; Split field data based on \n according to the event stream specification; Continue to read the data stream and wait for the server to disconnect or close manually.

7. A data push system based on SSE server, characterized in that: The system includes a server and a client. The server responds to the client's GET request, sets the response header Content-Type to text / event-stream, Cache-Control to no-cache, and Connection to keep-alive, and establishes a persistent HTTP connection. When data is updated, the server sends an event stream to the client through a persistent HTTP connection. The client uses a fetch or EventSource object to listen to and process the received event stream.

8. The data push system based on the SSE server according to claim 7 is characterized in that: The server-side implementation of SSE complies with the following specifications: Set SSE-related response headers (event stream, long connection, chunk transfer - event stream comes with and cache is disabled); the situation is as follows: Set the response header res.setHeader("Content-Type","text / event-stream;char set=utf-8"), the server-side response data type is the event stream of text / event-stream; Set the response header res.setHeader("Connection","keep-alive") to establish a keep-alive long connection between the client and the server; the response content will be divided into multiple blocks for transmission; Encapsulate data into events and send them to the client in the set format; Set the corresponding delay and buffer to control the sending timing; Disconnect clients at a set time; The client uses the fetch or EventSource object to listen to and process the received event stream as follows: Initiate a request: The client sends a GET request with a specified header to the server, indicating the type of event stream to receive and disabling any event cache; Receiving data: The client uses JavaScript's EventSource object to listen to the event stream sent by the server; when a new event arrives, the EventSource object triggers the corresponding event processing function according to the event type; Error handling and reconnection: The client handles connection errors by listening to the onerror event and automatically reconnects when necessary; The client uses the fetch method to call the service's interface and / or see, as follows: Get the readable stream object res.body; Get a reader object res.body.getReader(); Through the reader.read() method, the content of the response body is read asynchronously; Convert the data stream to a UTF-8 string; Split field data based on \n according to the event stream specification; Continue to read the data stream and wait for the server to disconnect or close manually.

9. An electronic device, characterized in that: include: memory and at least one processor; Wherein, the memory stores a computer program; The at least one processor executes the computer program stored in the memory, so that the at least one processor executes the data push method based on the SSE server as described in any one of claims 1 to 6.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which can be executed by a processor to implement the data push method based on the SSE server as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • Event stream processing method and device

    CN116800812A

  • Real-time message transmission method, server and browser

    CN116938859A

Cited By

  • Intelligent streaming data pushing method based on front-end performance optimization

    CN121173861A