SSE - is it efficient to handle SSE this way?

I just learned about SSE and it seems each article is explaining SSE using an interval to write data to the stream. isn't that going to cost more cpu and memory? especially that each request to the server is going to create another async function that is constantly checking for data change. how can I trigger a stream-write whenever a database write has been made or something similar that is more precise? how do I associate the session id with the actual user? can I include a cookie in the request?
7 Replies
Neto
Neto3y ago
HTTP/2 "HTTP/2 introduced support for multiplexing, meaning that multiple request/response pairs to the same host no longer require separate TCP connections. Instead, they all share the same TCP connection, each operating on its own independent HTTP/2 stream."
Neto
Neto3y ago
Server-Sent Events: the alternative to WebSockets you should be using
WebSockets are the most used technology for real-time web apps. However, Server Sent Events (SSE) are a simpler alternative that is often superior.
venego
venegoOP3y ago
this was a really good read, but I don't see how is that going to help me built my notification system.
Neto
Neto3y ago
store a map (key/value) with the key being the client and the value the connection when you need to notify the user access the key and use the connection from the value
venego
venegoOP3y ago
yh, but I need to know who that user is. so I can send the user specific notification. I should've explained this above. but the idea is, a user is giving another user the access to his account and I need to notify that user specifically about the access that he was given. I thought I just need the cookie to be sent to the server and then figure out if the user have some notifications in the queue or not. but it seems that SSE doesn't include the cookie in the request. so I need another way to verify who the client is to be able to notify the concerned client accurately. and how do you do that? do I just put a global reference to the function that writes to the stream?
Neto
Neto3y ago
Never did with sse But that is a way of doing with websockets You can also send to everyone and just listen to the correct event on the client
venego
venegoOP3y ago
oh ok. yes sending to everyone is the easiest. I just found out that the cookie is being sent to the server with the SSE request. my bad. I guess that's all what I needed. thanks tho.
Want results from more Discord servers?
Add your server