Saturday, June 10, 2017

How can I start / stop a request stream with node.js?

Leave a Comment

I have

return request({   method: "POST",   url: response.stream.url,   json: true,   forever: true,   body: {     sessionid: response.stream.sessionid,     symbols: symbolParams   } }).on("data", onData) 

That gives me data whenever it comes through. But what if I want to STOP listening on that stream, how can I do that?

3 Answers

Answers 1

It seems to implement Readable Stream interface, so pause() and resume() work.

const request = require('request'); const req = request('http://google.com')   .on('data', data => {        console.log(data);         req.pause();      }); 

Answers 2

You can use https://nodejs.org/api/events.html#events_emitter_removelistener_eventname_listener

const req = request({     method: "POST",     url: response.stream.url,     json: true,     forever: true,     body: {         sessionid: response.stream.sessionid,         symbols: symbolParams     } }).on("data", onData); ... req.removeEventListener('data', onData); 

Answers 3

You can also abort it:

var req = request('http://google.com')... ... req.abort(); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment