0

I would like to send back to the client a stream of responses from the npm twitter package.

The end-point looks like

var client = new Twitter({ consumer_key: '', consumer_secret: '', access_token_key: '', access_token_secret: '' }); app.get('/', function(req,res) { const stream = client.stream('statuses/filter', {track: 'crypto'}); stream.on('data', function(event) { res.status(200).json({tweet: event && event.text || 'nothing'}) }); }); 

The server responds once to the client app and then gives an error that headers are already sent Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client , how do I respond with a stream?

Thanks.

1 Answer 1

2

So, use

res.write(). 

It does not end it, so you can add call it multiple times, while after res.json & res.send you can't write to res more.

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

5 Comments

and call res.end() after you have sent all data.
Thanks @Muhammad Aadil Banara, and how do I get the response from the client side which is an angular app? I am now looking into socketio actuallu
No need to do any different thing for this, just call in the same way as we call in angular, one way to call in angular is like this. this.http.get('/app/test') .map((res:Response) => res.json()) .subscribe( data => { this.output = data}, err => console.error(err), () => console.log('done') );
OK let me try it out
Thanks but I have achieved using socket.io

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.