Front-End System Design

Server-Sent Events

Evgenii Ray

Evgenii Ray

Staff UI Engineer
Front-End System Design

Check out a free preview of the full Front-End System Design course

The "Server-Sent Events" Lesson is part of the full, Front-End System Design course featured in this preview video. Here's what you'd learn in this lesson:

Evgenii discusses server-sent events, which are one alternative to long polling. Server-sent events have automatic reconnection handling and are more battery-efficient. They are also a good alternative to Web Sockets and very effective for large text-data streams.

Preview
Close

Transcript from the "Server-Sent Events" Lesson

[00:00:00]
>> Eric: The first alternative that we could potentially use to log pooling is the Server-Sent Events. This is the server-push technology, so it's based on HTTP2. And the way it works is when you establish the connection with the server, the duplex communication is used only when you do the initial connection, just the initial three-way handshake.

[00:00:28]
But the rest of the connection is basically the server pushes the data to the client. So, in case of the Server-Sent Events, the duplex Santana is that used, we only use the receive only mode, and whether that drain the energy. Although, we still have an issue with the reconnection where we need to do the freeway handshake.

[00:00:50]
But the huge difference with the Server-Sent Events that the reconnection is implemented on protocol level. So you don't need to handle that on infrastructure wise, so you can always resume from the place where you started. And the huge benefit business-wise for using Server-Sent Events, it's very easy to scale.

[00:01:09]
So for instance if we have multiple instances of the server and one instance is died, we can just forward the request to a separate instance. And because we can always resume from the previous place using the Server-Sent Events, so it doesn't matter for us which instance will respond.

[00:01:31]
So since the Server-Sent Events is HTTP2-based, it also utilizes the TCP socket differently. We gonna talk about HTTP2 protocol just in a few moments, but the huge benefit of HTTP2 it has the multiplexing feature. The multiplexing allows us to open 200 requests within one single TCP socket. So this means while in order to be one to fetch five resources, we have to open five TCP connection, which is much less efficient.

[00:02:09]
So the Server-Sent Events is generally much faster than long polling because it also doesn't send any channel data. So we don't send any unnecessary headers and receive only the data that relates to the content. And the summary for that is there are definitely pros for Server-Sent Events because the connections can handled thematically, it's very better, efficient from the mobile device perspective.

[00:02:33]
And it doesn't send the overhead data and the horizontal scaling of your infrastructure is much easier when you use the Server-Sent Events. The cons though, you can't push the data to a server and only the string data is supported by detraining this. So you will need to parse the text payload that you send from the server.

[00:02:56]
And when you need to use it, that's good for both desktop and mobile applications, and it also provides a very good performance. So the performance comparable to the web sockets that we're going to discuss on the next slide. And it's very good for streaming the large text data, if you have this case.

[00:03:16]
And the reason why you'd avoid this, is if you are building very simple desktop app, so then you'll be fine with long poll. You don't need to really overcomplicate the architecture of your app.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now