R
Railway•8mo ago
Gabo Bermudez

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
"scripts": {
"start": "node index.js"
},
"scripts": {
"start": "node index.js"
},
These are my dependencies:
"dependencies": {
"axios": "^1.4.0",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"pg": "^8.11.3",
"qs": "^6.11.2",
"redis": "^4.6.7",
"sequelize": "^6.32.1",
"xml-js": "^1.6.11"
},
"dependencies": {
"axios": "^1.4.0",
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"pg": "^8.11.3",
"qs": "^6.11.2",
"redis": "^4.6.7",
"sequelize": "^6.32.1",
"xml-js": "^1.6.11"
},
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.
No description
35 Replies
Percy
Percy•8mo ago
Project ID: 81fdcea2-2cc1-4581-a362-97564aa3a6d7
Brody
Brody•8mo ago
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?
Gabo Bermudez
Gabo Bermudez•8mo ago
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?
Brody
Brody•8mo ago
well for websockets you dont even need a custom domain, that article is very old and no longer an issue
Gabo Bermudez
Gabo Bermudez•8mo ago
This is how I'm responding on the server
res.setHeader('Content-Type', 'text/event-stream')
res.setHeader('Cache-Control', 'no-cache')
res.setHeader('Connection', 'keep-alive')
res.flushHeaders()

let isConnectionOpen = true

req.on('close', () => {
res.end()
isConnectionOpen = false
})
res.setHeader('Content-Type', 'text/event-stream')
res.setHeader('Cache-Control', 'no-cache')
res.setHeader('Connection', 'keep-alive')
res.flushHeaders()

let isConnectionOpen = true

req.on('close', () => {
res.end()
isConnectionOpen = false
})
I send the message using
res.write(`data: {"progress": ${progressRoundUp > 100 ? 100 : progressRoundUp}}\n\n`)
res.write(`data: {"progress": ${progressRoundUp > 100 ? 100 : progressRoundUp}}\n\n`)
And close it at the end of it (it it didn't close on the client first) using
res.end()
res.end()
Brody
Brody•8mo ago
can you share a link to where i could test your sse endpoint? similar to my sse link
Brody
Brody•8mo ago
does this work locally?
Gabo Bermudez
Gabo Bermudez•8mo ago
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
Brody
Brody•8mo ago
im only getting a single message when using that link you just sent
Gabo Bermudez
Gabo Bermudez•8mo ago
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
Brody
Brody•8mo ago
okay its counting coming up on a minute and still connected
Gabo Bermudez
Gabo Bermudez•8mo ago
That's more than I got
Brody
Brody•8mo ago
stuck at 27 but still connected
Gabo Bermudez
Gabo Bermudez•8mo ago
I can see the logs stuck It looks like it just stopped logging
Brody
Brody•8mo ago
looks like you have some debugging to do coming up on 3 minutes and still connected conncetion closed after 3 minutes and 6 seconds
Gabo Bermudez
Gabo Bermudez•8mo ago
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?
Brody
Brody•8mo ago
railway would not close idle connections that fast yeah this would be a code issue
Gabo Bermudez
Gabo Bermudez•8mo ago
huh, so definitely a code issue
Brody
Brody•8mo ago
definitely
Gabo Bermudez
Gabo Bermudez•8mo ago
Ok, sounds good. Thanks very much for you help. I'll do some debugging and post here the solution for future reference.
Brody
Brody•8mo ago
i look forward to seeing what was wrong 🙂
Gabo Bermudez
Gabo Bermudez•8mo ago
Me too. I look forward to being embarrassed by –probably– a very obvious issue 🙃
Brody
Brody•8mo ago
thats coding for you 🤣
Gabo Bermudez
Gabo Bermudez•8mo ago
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?
Brody
Brody•8mo ago
railway does not have any such restrictions what 3rd party api are you trying to call?
Gabo Bermudez
Gabo Bermudez•8mo ago
Then, I've found myself in a really odd situation. It's a POS system called LAVU.
Brody
Brody•8mo ago
very odd situation indeed
Gabo Bermudez
Gabo Bermudez•8mo ago
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
Brody
Brody•8mo ago
say name
Gabo Bermudez
Gabo Bermudez•8mo ago
Heroku
Brody
Brody•8mo ago
they are monkey patching your code railway runs your code as is
Gabo Bermudez
Gabo Bermudez•8mo ago
oh, really? Well, dang... I'm still on the hunt, then
Brody
Brody•8mo ago
as you can see, absoletely nothing wrong with sse on railway wish i knew what was wrong with your code though, sorry
Gabo Bermudez
Gabo Bermudez•8mo ago
Yeah, no worries. Thanks for the help