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.
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.