Introduction
Hey fellow devs! π Today, let's dive into Server-Sent Events (SSE) and explore why they might be your next favorite tool fo...
For further actions, you may consider blocking this person and/or reporting abuse
A warning: you're probably going to still need some kind of polling fallback. These days I check for receipt acknowledgement and then use the SSE stream for polling, which works, but the server has to close the connection if it doesn't get an "ack".
Server Sent Events are still not production ready after a decade. A lesson for me, a warning for you!
Mike Talbot β γ» Jun 6 '20
Cool, thanks for sharing! I will read it. π
True. I am still struggling to configure nginx properly for SSE
These are the headers I send
It's amazing that I've been at this for 15+ years and I'm still discovering new things like server-sent events. It was proposed 20 years ago already and somehow I've missed it completely. Simply never bumped into it.
Thanks for this post!
Glad this post brought it to your attention! Do you think you'll use SSE in any of your projects?
The title of the post is very misleading. You might get excited when you learn new things that doesn't mean "Saying goodbye to something", just for views don't do such nonsense.
Not everything has to be taken so seriouslyβsometimes a catchy title is just that. Take it easy!
Click bait is always click bait.
I dont think this will replace the websockets, its just another technology for different use cases. SSE is for one way comunication and is more related to send notifications between the client and the server (for example in my job we have a react app who run inside of a 3rd party old electron app who has our app inside of an iframe and we needed to use SSE for sending an event to revoke all cookies in the electron application). If I need a chat or something "in real time" id still prefer to use web socket, things like push notifications SSE is the best choice
Of course, it's not a replacement because they serve different purposes. But if you want to deal with real-time data, you should consider SSE first. If you need a more complex, duplex channel, then WebSockets are the way to go!
witching from WebSockets to SSE sounds like a big shift! SSEβs simplicity and lower overhead make it a strong alternative, especially for real-time updates. Curious to see how it holds up in high-demand applications!
Try it and tell us π
It's just a brodcaster?
How the authorisation works here?
If there is 100 users , everyone will get everyones data??
I'm sorry I'm just a noob trying new things
Great questions! SSE isn't just a broadcasterβit's a way for the server to push updates to clients in real-time over a single HTTP connection.
As for authorization, it usually works by validating the user's identity before establishing the SSE connection, just like any other secure connection.
Regarding your last question: If you have 100 users, each client will only receive the data that the server sends to it. Typically, you would send specific updates based on the userβs permissions or the data theyβre subscribed to, so not everyone gets everyone else's data.
@5p4r70n , you would have to use your existing auth model. For example, we use sse to send notifications to users of our system when they are in the admin console. The app uses tokens so the server only receives authenticated messages.
Also since we have info on the user via the token we store some identifier info on the cached connection pool so when a SSE is generated we can send only to clients that match the recipient id.
SSE is simplest described as a regular HTTP request with a streaming response; the streaming payload is events.
WS requests on the other hand convert (βupgradeβ) the HTTP request to something outside of the HTTP spec to get access to the sockets that the original HTTP request was using.
In both cases the server has to manage a distinct connection to each user; however SSE connections tend to be less demanding on server resources and SSE may work where WS won't; like when firewalls block non-HTTP traffic. But both require an always-on server,so neither of them is suitable for a serverless environment.
Ok, go it
The server and client will keep this connection alive for future server-to-client event notifications.
Thanks! π
Few days back, we got a requirement where our DAO layer is collecting data from some files in the backend, as now this data can be large and we cannot load the complete data in memory hence we thought of going with SSE where server can send the data in chunks to Client in stream without loading the complete data.
In this case also SSE played a good role to send the data continuously without waiting to load in memory at once.
Websocket can be also used for this purpose but here we dont want duplex channel hence SSE played a vital role.
Thanks for sharing your experience!
Using SSE as been a new pattern for me. After using tools like HTMX, I feel in love with sending back fragments of HTML.
Then I found Datastar data-star.dev/
It's a blend of JS signals, HTMX, and SSE.
Have you used it in a real project? I mean "Data star"?
Internal tools only. It's still in beta with v1 on the horizon.
π
Pretty solid, btw how did you create the blog image?
Thanks man! For the cover it's just AI generated.
thanks man
You're welcome man!
thanks
You are welcome!
Server-Sent Events (SSE) can be a game-changer for real-time web applications, especially for use cases like notifications, live updates, and monitoring dashboards. Unlike WebSockets, SSE operates over a single HTTP connection and is inherently more efficient for unidirectional data streaming. However, when integrating SSE with GenAI applications, security risks must be carefully considered. Exposing AI-generated content through SSE could lead to data leakage, injection attacks, or unauthorized access if not properly secured. Implementing authentication, rate limiting, and content validation is essential to mitigate these risks.
Okay more seaport to back and
this requires http2, right ? SSE requires server-push which are available after http2
Actually, SSE doesn't require
HTTP/2
. It's designed to work overHTTP/1.1
as well. Unlike HTTP/2's server push, which allows the server to proactively push resources to the client, SSE uses a single open connection over HTTP to stream events to the client. The key here is that SSE works through a long-lived connection, but doesn't require the features of HTTP/2 like multiplexing or server push.@ffreality , SSE works with regular http, however, there is a connection limitation as @dylan_westraadt_92f0c807d mentioned. If using http/2 then the connection limit can be negotiated.
can we get same SSE for python based backends???
Yes, Just an HTTP GET request with specific headers π
SSE has a limit of 6 connections per browser window. Just something to keep in mind. Its not a one size fits all solution.
It's 6 connections (i.e. requests, not just SSE) per domain across the entire browser for HTTP/1.x. The limit is a lot higher for HTTP/2.
That said architecturally the plan would be to multiplex all traffic over a single SSE for the entire page and for an MPA to use leader election where only one page of the domain would use an SSE and redistribute the events to the other pages from the same domain via
BroadcastChannel
.Yes, But you can use one connection with multiple events.
Hi
I tried SSE in my project. It worked great but I was not able to configure nginx properly for this.
If you have any suggestions please let me know.
Thanks in advance.
SSE requires keeping the connection open for a longer time, so you need to make sure NGINX is configured to handle that.
Here are a few suggestions:
Keep the connection alive: Make sure the server and NGINX donβt timeout prematurely while the connection is open. The
proxy_read_timeout
andproxy_send_timeout
parameters are key here.Turn off buffering: SSE requires that data be pushed to the client immediately, so turning off buffering in NGINX with
proxy_buffering off
is important for real-time delivery.Whatch this video:
youtu.be/qoEq_Ro1wjU