This post is a mirror of a post I wrote on my own blog. If you would like python or native javascript examples of the code I presented below, feel ...
For further actions, you may consider blocking this person and/or reporting abuse
Excellent post! This is the first time I've heard of SSE. I don't know how I haven't come across this before. WebSockets are my usual go to for this sort of operation, however, if the client does not need to send any messages back to the server, then it seems SSE is the obvious choice. The automatic reconnection is very cool π.
The only thing that I can see that may be a problem is the browser compatibility of SSE vs WebSockets (but I guess polyfills are there to be used).
Thank you!
As for support, when building a production application, you should look into this polyfill:
github.com/Yaffle/EventSource
Hey Shubham,
Thanks for the article, learned a lot!
I'm having a weird issue where the server is getting hit and emitting responses, but it takes a couple minutes for the client to receive them.
Any idea what could account for this?
Couple minutes ? That is interesting, is this your own server or is it the server I set up?
I would say if itβs not my server, it might be due to the size of the data you are sending to the client. If you have a code sample to share, please do
Ive done a lot of development but this is my first node app using express.
Ive implemented your back end as is in a middleware component on my node server app. However whenever is issue the server.get call my main app.js throws an ERR_HTTP_INVALID_STATUS_CODE error after running through all of the app.use methods that redirect api responses.
I wonder if you have time to look at my code. Im sure Im making a rookie mistake.
Bryan
Hi Bryan!
Sorry for the lack of response, Iβd love to look at your code, send me a dm of your git repo and Iβll check it out if youβre still having this issue.
Its not up yet on git since its not working. also the repo is in another fellows name...
I started to try and solve the server-sent-events issues using express-sse.
That is throwing a 404 error. I will send you the stackoverflow question once i get it entered.
Finally decided to just do a specific implementation outside of my other project. I put it up as public at
github.com/bryanforst/sse-test
The client is still throwing a 404 error - odd thing is that its just showing localhost:3000 and not the endpoint /sseconnect.
I am sure its something stupid I am missing. Thanks for any advice you can give
I think you might be running two servers that are both fighting for port 3000. There is a server running hosting your react app and a server running hosting your SSE events.
There are two ways we can solve this problem:
Changing one of the ports to a different port.
This is pretty simple, just change the port of your SSE server to 3001 instead of 3000 and change the url you are connecting to to
http://localhost:3001/server-sent-events-url
Hosting the server code and the react code on the same server
I think this article has some good info on running an express server with react.
dev.to/dan_mcm_/leveling-up-from-c...
I'll also see if I have some time to add a react example to this codebase so people can better try it out end to end with react.
Thanks Shubham,
Originally in my app the back end was listening on 5000 with the front end on 3000. When I attempted to set EventSource to the back end I was getting an error message. That confused me.
Indeed after setting the back end in my test to listen at 5000 all worked well.
Will try to integrate this into my main app.
Thanks for all your help so far
bryan
Perfect! Glad it worked out for you
Hi,
Thank you for the tutorial, I learned a lot.
Hi there, sorry for late response!
res.end()
when you want to close the connection.Best,
Shubham
Hi Shubham, thank you for your reply
Regards,
Andri
You could use an SSE connection for this yes, and then you could close the connection when value = 100?
Oh I see, yes thank you for your reply
Hi Shubham, really nice article.
I'm starting to have a look to SSE and I was wondering if there's a way to have something like "channels" to stream only to certain users and to keep those "user sessions" alive to send messages, so far I've seen arrays to keep a user list but that's not very real life approach... any idea on how this would work? Something like redis could be useful? How do I keep the session when sending a message in the future should I have those connections in my server memory?