Server-Sent Events on NodeJS+Express not working
Project ID: 81fdcea2-2cc1-4581-a362-97564aa3a6d7
So, I have this NodeJS+Express REST API which has an endpoint that performs a time-consuming task. For this reason I'm using Server-Sent Events to send a progress update to the client.
Locally, it all worked fine but was closing the connection after a few seconds once I deployed it on Railway.
Looking through the KB I noticed the WebSockets issue with wildcard domains (I was using one), so I bought a domain and setup a custom domain for this API. It's still failing 😦
I'm not sure if there's a configuration to be made for streaming events that I'm missing or something of that sort.
I'm running my application with
npm start
These are my dependencies:
I attached what my Network tab looks like from the browser.
The API is supposed to be sending messages every couple of seconds, so it's not a default HTTP timeout that could be easily solved with keep-alive messages.
Any ideas of what might be going on? Any further information needed, I'll provide.
Thanks in advance.35 Replies
Project ID:
81fdcea2-2cc1-4581-a362-97564aa3a6d7
server sent events and websockets are nothing alike
and there is no timeouts for server sent events, you can watch this until the cows come home https://utilities.up.railway.app/sse
this would be a problem with your code
how are you doing sse?
I know there are differences between the two, but thought that holding the connection up was the culprit. Seems like it's not. So, for SSE I don't need a custom domain?
well for websockets you dont even need a custom domain, that article is very old and no longer an issue
This is how I'm responding on the server
I send the message using
And close it at the end of it (it it didn't close on the client first) using
can you share a link to where i could test your sse endpoint?
similar to my sse link
does this work locally?
When I put it directly on the browser it does send more messages –it sent about 25 messages, instead of 9.
Yes, let me double check
im only getting a single message when using that link you just sent
Could you try again? It's a time-sensitive task so probably it's up-to-date and doesn't need to perform anything yet. I just updated the timeframe so that it sends about 60 messages.
Locally works like a charm
okay its counting
coming up on a minute and still connected
That's more than I got
stuck at 27 but still connected
I can see the logs stuck
It looks like it just stopped logging
looks like you have some debugging to do
coming up on 3 minutes and still connected
conncetion closed after 3 minutes and 6 seconds
Probably closed because it was idle?
Meaning... it's expected and not an infrastructure issue. And if so, probably I need to check my code?
railway would not close idle connections that fast
yeah this would be a code issue
huh, so definitely a code issue
definitely
Ok, sounds good.
Thanks very much for you help. I'll do some debugging and post here the solution for future reference.
i look forward to seeing what was wrong 🙂
Me too. I look forward to being embarrassed by –probably– a very obvious issue 🙃
thats coding for you 🤣
I've noticed something!
It's always getting stuck before it makes an API call to a third party REST API. The third party API does not have any query per minute restrictions, I made sure of it.
Does Railway might be limiting in some way the requests that go out to an external API?
railway does not have any such restrictions
what 3rd party api are you trying to call?
Then, I've found myself in a really odd situation.
It's a POS system called LAVU.
very odd situation indeed
So, funny thing... I tried a different platform (not sure if I can say their name) and it worked with no issues. The exact same code
say name
Heroku
they are monkey patching your code
railway runs your code as is
oh, really? Well, dang... I'm still on the hunt, then
as you can see, absoletely nothing wrong with sse on railway
wish i knew what was wrong with your code though, sorry
Yeah, no worries. Thanks for the help