Skip to main content

Posts

Showing posts with the label Http

Express not allowing more than one pending request from the same IP?

I'm making a chat application that uses long polling with express (I'm aware websockets are better for this, I just wanted to make something specifically with long polling). This is the code that the client sends a GET request to when it's waiting for a new chat message: app.get('/api',(req,res)=>{ listeners.push(res) }) It pushes the response objects to an array, and once a new chat message is available it calls .send() on the responses. When the client recieves the response, it sends a GET request again, waiting for new messages. Here's the code for when someone sends a message (simplified) app.post('/api', (req,res)=>{ listeners.forEach(listener=>{ listener.json(req.body) }) listeners = [] res.status(200).end() }) I noticed that when I open two tabs of my site, only the 1st tab's request gets put in the array at first. That makes the 2nd tab not receive the 1st chat message. The 2nd tab's reque...