Keep Node JS Connection Opened And Write (GET Request)

- 1 answer

I was trying to implement a NODE JS get method where I could encode in the url parameters and send back responses like in Server Sent Events. For example, when I used:

curl -D- 'http://localhost:8030/update'

The server would return a message, and then keep the connection opened to return more messages (like Push). I was using require('connect'), I tried with require('express') but can't get it working.

Here's part of my code:

var http        = require('http');
var connect     = require('express');
var app         = connect();

app.use(bodyParser.urlencoded({ extended: false })) 
.use(bodyParser.json()) // JSON
.get('/update', updateMiddleware);

var server = http.createServer(app);

server.listen("", 8030);

function updateMiddleware(req, res) {
   res.setHeader('Connection', 'keep-alive');
   res.setHeader('Content-Type', 'text/event-stream');
   res.setHeader('Cache-Control', 'no-cache');
   setTimeout(function() {
       res.write("this is an event");
   }, 1000);
   setTimeout(function() {
       res.write("this is another event");
   }, 2000);
   // should print without ending

EDIT: I found it was working, but only in chrome. In terminal, I only receive it after waiting a long time, and messages come like in chunks.



You can't use a single HTTP request to listen for multiple event data. If you are really stuck with HTTP (i.e. WebSocket or WebRTC is not an option) then the technique you are looking for is called long polling. This basically works this way:

  • Client sends request to server
  • Server waits until an event happens (or until a specific but not too long timeout, so the client application does not throw a timeout error for the request)
  • Server responses with a complete http response, containing the details of the event
  • Client receives the event details and immediately sends another request to listen to further events.

This method really takes advantage of HTTP Keep-Alive


For me it looks like your code does not follow the protocol of server sent events. Here is a tutorial: Server-Sent Events in nodejs.

Following another tutorial on MDN about Server-Sent Events, the structure of the messages should be the following:

: this is a test stream

data: some text

data: another message
data: with two lines

Note that the data to be sent must be followed by a double new-line \n\n.