SoFunction
Updated on 2025-02-28

How to use post to make eventSource requests in front-end

Preface

In our daily work, we may need to establish a link with the server to accept data pushed by the server. The common use is eventSource. What we usually use is to create an eventSource through get, but how do we create it through post? First, let’s introduce eventSource, and the difference between it and websocket:

Introduction

1) eventSource is a Web API that allows web pages to receive automatic updates from a web server over long HTTP connections (commonly known as Server-Sent Events (SSE). It allows web pages to receive real-time updates without refreshing the page or sending duplicate requests to the server.

2) Using eventSource, web pages can subscribe to event streams sent from the server. These events can be in any format, such as plain text, JSON, or XML, and can contain any data the server wants to send. Once a connection is established, the server can send events to the client at any time, and the client can handle them as needed, such as updating the UI or triggering other actions.

3) The eventSource API is simple to use and is supported by most modern web browsers. It is often used in web applications that require real-time updates, such as chat rooms, social media feeds, or stock market quotes.

The difference between websocket:

1) Different protocols: WebSocket uses a two-way communication protocol, while eventSource uses a one-way communication protocol. The WebSocket protocol can establish a long connection between the client and the server, and both parties can send and receive messages at the same time, while eventSource can only send messages to the client by the server.

2) Different data formats: WebSocket can send data in any format, such as text, binary data, or JSON, while eventSource can only send data in text format.

3) Different levels of support: WebSocket is a relatively new technology that may not be supported in some old browsers or network environments, while eventSource has been widely supported and can be used in most modern browsers.

4) Different application scenarios: WebSocket is more suitable for applications that require real-time two-way communication, such as online games or video conferencing, while eventSource is more suitable for applications that require real-time information from the server, such as stock quotes or news push.

3. How to request eventSource in post

The commonly used one is to implement it through the fetchEventSource library, and the implementation method is as follows:

npm i --save @rangermauve/fetch-event-source
 
import { fetchEventSource } from '@microsoft/fetch-event-source';
 
       let eventSource = fetchEventSource(Url, {
        method: 'POST',
        headers: {
          "Content-Type": 'application/json',
        },
        body: (data),
        onmessage(event) {
          ();
        },
        onerror() {
          
        }
      })

Summarize

This is the article about how to use post to make eventSource requests in front-end. For more relevant post methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!