Server-Sent Events Is it http2 server push?

发布时间 2023-03-29 23:08:19作者: 让速不让路

Person:

  • Please give me an example where gin implements server push and the front end receives data.

ChatGPT:

  • Sure! Here's an example of how you could implement server push using Gin and receive data on the front-end:

Let's say you want to push updates about new messages to a chat application. On the server side, you can use the sse (Server-Sent Events) package in Gin to send real-time updates to the front-end.

First, you'll need to import the necessary packages:

import (
    "net/http"

    "github.com/gin-gonic/gin"
    "github.com/gin-contrib/sse"
)

Then, you can create a route that sends server-sent events to the front-end:

func streamHandler(c *gin.Context) {
    c.Stream(func(w io.Writer) bool {
        c.Render(-1, sse.Event{
            Event: "message",
            Data:  []byte("New message received"),
        })
        time.Sleep(1 * time.Second) // simulate a delay between events
        return true
    })
}

In this example, the streamHandler function sends a server-sent event with the event type message and the data New message received. It then waits for 1 second before sending the next event.

On the front-end, you can use the EventSource API to receive the server-sent events:

const eventSource = new EventSource('/stream');

eventSource.addEventListener('message', (event) => {
  console.log(event.data);
});

This code creates a new EventSource instance that connects to the /stream route on the server. It then listens for the message event and logs the data received from the server.

When a new message is received on the server, the streamHandler function sends a server-sent event to the front-end, which is received by the EventSource instance and logged to the console.

Person:

  • What's behind Server-Sent Events? Is it http2 server push or websocket?

ChatGPT:

  • Server-Sent Events (SSE) is a server-side technology that allows for real-time, push-based communication between a server and a client over a single HTTP/HTTPS connection. Unlike WebSockets, SSE uses the HTTP/HTTPS protocol, so it works over standard ports and doesn't require any special server configuration.

Under the hood, SSE uses a long-lived HTTP connection between the client and server. The client sends an HTTP request to the server, and the server keeps the connection open and sends events to the client as they occur. The events are sent as plain text or JSON data in a specific SSE format, which includes an event type, an optional event ID, and the event data.

HTTP/2 Server Push is a different technology that allows a server to proactively send resources to a client before the client requests them. With HTTP/2 Server Push, the server can send resources like CSS, JavaScript, or images to the client along with the initial HTML page, without waiting for the client to request them. This can improve the page load time and reduce latency.

While SSE and HTTP/2 Server Push both enable real-time, push-based communication between a server and a client, they work in different ways and have different use cases. SSE is best suited for applications that need to send a small number of real-time events from the server to the client, such as chat applications or real-time monitoring dashboards. HTTP/2 Server Push, on the other hand, is best suited for optimizing the initial page load and reducing latency for static resources on a website.