DEV Community

Cover image for Building with server-sent events with React and Node.js

Building with server-sent events with React and Node.js

Shubham Naik on June 09, 2020

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 ...
Collapse
 
gandalfarcade profile image
Chris Mumford

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).

Collapse
 
4shub profile image
Shubham Naik

Thank you!

As for support, when building a production application, you should look into this polyfill:
github.com/Yaffle/EventSource

Collapse
 
codechung profile image
CodeChung

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?

Collapse
 
4shub profile image
Shubham Naik • Edited

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

Collapse
 
bryanforst profile image
bryan forst

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

Collapse
 
4shub profile image
Shubham Naik

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.

Collapse
 
bryanforst profile image
bryan forst

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.

Thread Thread
 
bryanforst profile image
bryan forst

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

Thread Thread
 
4shub profile image
Shubham Naik • Edited

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:

  1. 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

  2. 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.

Thread Thread
 
bryanforst profile image
bryan forst

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

Thread Thread
 
4shub profile image
Shubham Naik

Perfect! Glad it worked out for you

Collapse
 
wsh4and profile image
Eko Andri Subarnanto

Hi,

  1. How do you do sse if you want to wait for the long running query to be completed?
  2. How do you close connection if you already receive the completed data?

Thank you for the tutorial, I learned a lot.

Collapse
 
4shub profile image
Shubham Naik

Hi there, sorry for late response!

  1. I am not sure, could you rephrase this question?
  2. you should invoke res.end() when you want to close the connection.

Best,
Shubham

Collapse
 
wsh4and profile image
Eko Andri Subarnanto

Hi Shubham, thank you for your reply

  1. Let's say that I want to perform a long running query in the backend, and I want to inform the UI the percentage of how much it is done, say 10%, 20% and so on. Is it through a sse connection or with something else? So the backend keeps sending the 'value' of how much the query has been done? And which part close the connection, UI atau backend?
  2. Thank you.

Regards,
Andri

Thread Thread
 
4shub profile image
Shubham Naik

You could use an SSE connection for this yes, and then you could close the connection when value = 100?

Thread Thread
 
wsh4and profile image
Eko Andri Subarnanto

Oh I see, yes thank you for your reply

Collapse
 
soniar4i profile image
Sonia C.

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?