Search code examples
node.jstwitter

nodejs (twit) how do you follow a users LIVE twitter stream


I am trying to get twit (https://www.npmjs.com/package/twit) to stream live tweets from specific twitter users.

The code I have is

var stream = T.stream('statuses/filter', { follow: ['nodejs'] })

stream.on('tweet', function (tweet) {
    console.log(tweet.text)
})

but it gives the following error

Error: Bad Twitter streaming request: 406
    at Object.exports.makeTwitError (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/twit/lib/helpers.js:74:13)
    at Request.<anonymous> (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/twit/lib/streaming-api-connection.js:96:29)
    at Request.emit (events.js:187:15)
    at Gunzip.<anonymous> (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:273:13)
    at Gunzip.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Request.<anonymous> (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/twit/lib/streaming-api-connection.js:99:14)
    at Request.emit (events.js:187:15)
    [... lines matching original stack trace ...]
    at process._tickCallback (internal/process/next_tick.js:63:19)

simply tracking words in a tweet works just fine, for instance the following code works OK.

var stream = T.stream('statuses/filter', { track: ['apples'] })

stream.on('tweet', function (tweet) {
    console.log(tweet.text)
})

The twitter documentation suggests that I should be able to simply replace track with follow.

https://developer.twitter.com/en/docs/tweets/filter-realtime/api-reference/post-statuses-filter.html


Solution

  • You need to use the numeric user ID, not the screen name, in the follow parameter. You can use the users/show endpoint to find the user ID for a given screen name.