0

I'm working in NodeJs and I'm making a JSONClient request with restify to an API and I want to send the text/event-stream header in order to suscribe me to the server responses.

A common call to the API without that header looks like this:

var client = restify.createJSONClient({
    url: 'http://api.example.com'
});

return client.get('/getResource', 
        function (err, req, res, obj) {
   //Do stuff            
}

Doing this I get an answer but it's not the final one, for example I could get "working", "busy" or "doing-something" so I want to suscribe me to the server responses and when the response will finish then "do the stuff I need with the data".

Is there a way to send the headers on a get request (JSONClient) in restify?

I'd tried something like this but seems not to be working

client.headers["content-type"] = 'text/event-stream';

I don't know if I'm been clear enough but I need the equivalent to:

curl -H "accept:text/event-stream" http://api.example.com/getResource

By the way, I'm stuck with restify since I'm using node-workflow.

I want to avoid making polling as possible.

2 Answers 2

0

I couldn't solve it with restify, it seems that they don't implemented, otherwise I solved with eventsource and I want to share that solution:

var es = new EventSource('http://api.example.com/createAccount');
es.on('status', function(e) {
  // status event
  console.log(e.data);
}).on('result', function(e) {
  // result event
  console.log(e.data);
}).on('error', function() {
  console.log('ERROR!');
});

In this way the streaming is infinite and when the server stops responding the error function is called (infinite times). Note that we don't need to set the headers. It's something that eventsource already does.

Thanks to @mscdex who answer in another question.

Sign up to request clarification or add additional context in comments.

Comments

0

I know this is an old question, but I was able to get a restify server working with Server Sent Events, by adding the following after creating the server:

server.acceptable.push('text/event-stream');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.