Multiple calls to Twitter streaming API in Node.js using ntwitter

I have the following code:

var access_token_key, access_token_secret, consumer_key, consumer_secret, get_twitter, listen, twitter;

twitter = require('ntwitter');

consumer_key = 'bogus';
consumer_secret = 'fake';

// consumer
access_token_key = 'falsey';
access_token_secret = 'unreal';

get_twitter = function(key, secret) {
  return new twitter({
    consumer_key: consumer_key,
    consumer_secret: consumer_secret,
    access_token_key: key,
    access_token_secret: secret

listen = function(feed) {
  var api = get_twitter(access_token_key, access_token_secret);
  return'statuses/filter', {track: feed.track}, function(stream) {
    console.log('setting up stream for %s', feed.track);        

    stream.on('data', function(data) {
      console.log('data for %s', feed.track);
      console.log('t' + data.user.screen_name + ': ' + data.text);

    stream.on('end', function(response) {
      console.log('ending stream for %s', feed.track);

    stream.on('destroy', function(response) {
      console.log('destroying stream for %s', feed.track);

module.exports.listen = listen;

When I call listen({track: 'superbowl'})
it works. Hurray! It does everything a little app should! I get this in the console:

setting up stream for superbowl
data for superbowl
    user: tweet

But after that, I call listen({track: 'humble bundle'})
, it sets up the new stream, but this happens:

setting up stream for humble bundle
ending stream for superbowl
destroying stream for superbowl
data for humble bundle
     user: tweet

Am I doing something incorrectly here? Is there some concept of Node or of Twitter that I have not yet grasped fully?

Problem courtesy of: Brian Hicks


From the Twitter streaming API docs ( under Access and Rate Limiting:

Each account may create only one standing connection to the Streaming API. Subsequent connections from the same account may cause previously established connections to be disconnected. Excessive connection attempts, regardless of success, will result in an automatic ban of the client's IP address. Continually failing connections will result in your IP address being blacklisted from all Twitter access.

I.e., you can only have one streaming connection at a time. In order to receive both sets of results you need to instead use listen({track: 'superbowl, humble bundle'})
. This means that you can’t change what you are listening to and will have to stop listening, modify the query and restart listening instead.

Solution courtesy of: Colin Ross

Node.js Recipes责编内容来自:Node.js Recipes (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Multiple calls to Twitter streaming API in Node.js using ntwitter

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录