question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Streams unreliable for collecting tweets from slow tweeting accounts

See original GitHub issue

Prerequisites

  • Put an X between the brackets on this line if you have done all of the following:
    • Checked that your issue isn’t already filed: [open issues][open-issues]
    • Checked the [bug report guidelines][bug-report-guidelines]

Description

The streams part of twitter-v2 is unreliable. I’ve set it to monitor five twitter accounts that tweet about once every six hours.

Steps to Reproduce

  1. Watch several accounts in a filtered stream that don’t post that often.
  2. Start streaming tweets
  3. Streams randomly quit within 30-45 minutes or less.

Expected behaviour:

Should be able to run indefinitely. twitter-v2 should not disconnect the stream just because it thinks the stream is unresponsive. The sample code by Twitter themselves does not have this behaviour.

Actual behaviour:

Streams randomly hit this._closeWithError.

    at Timeout.<anonymous> (/home/rctgamer3/project/node_modules/twitter-v2/src/TwitterStream.js:50:30)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

Stream timeout’s, and throws a TwitterError that the stream is unresponsive. Reproduces how often: Always, the only variable is the time until it closes the stream.

Versions

1.0.8

Additional Information

If you need any further information to triage things, let me know.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
gustawdanielcommented, Jun 23, 2021

This is code to reproduce:

import TwitterStream from "twitter-v2/build/TwitterStream";

const Twitter = require('twitter-v2');

const client = new Twitter({
    bearer_token: process.env.TWITTER_API_BEARER_TOKEN,
});

// client.get('tweets/search/stream/rules').then(console.log);

const listenForever = async (
    streamFactory: () => TwitterStream,
    dataConsumer: (data: any) => void
): Promise<any> => {
    try {
        for await (const {data} of streamFactory()) {
            dataConsumer(data);
        }
        // The stream has been closed by Twitter. It is usually safe to reconnect.
        console.log('Stream disconnected healthily. Reconnecting.');
        return listenForever(streamFactory, dataConsumer);
    } catch (error) {
        // An error occurred so we reconnect to the stream. Note that we should
        // probably have retry logic here to prevent reconnection after a number of
        // closely timed failures (may indicate a problem that is not downstream).
        console.warn('Stream disconnected with error. Retrying.', error);
        return listenForever(streamFactory, dataConsumer);
    }
}

listenForever(
    () => client.stream('tweets/search/stream'),
    (data) => console.log(data)
).catch(console.error);

And this is results:

{
  id: '1407654882488823808',
  text: 'GET https://t.co/qnn5a0VaXe for check rules of filtering messages on twitter.'
}
Stream disconnected with error. Retrying. TwitterError: Stream unresponsive
    at Timeout.<anonymous> (/media/nvme/pro/crypto/twitter-producer/node_modules/twitter-v2/build/TwitterStream.js:43:38)
    at listOnTimeout (node:internal/timers:556:17)
    at processTimers (node:internal/timers:499:7)
Stream disconnected with error. Retrying. TwitterError: Stream unresponsive
    at Timeout.<anonymous> (/media/nvme/pro/crypto/twitter-producer/node_modules/twitter-v2/build/TwitterStream.js:43:38)
    at listOnTimeout (node:internal/timers:556:17)
    at processTimers (node:internal/timers:499:7)

First json when I posted tweet:

https://twitter.com/PreciseLabPL/status/1407654882488823808

Next error some minutes after my tweet and next error some minutes after.

There is connected topic:

https://twittercommunity.com/t/filtered-stream-disconnects-every-5-minutes-on-nodejs/153866/7

My rules:

https://api.twitter.com/2/tweets/search/stream/rules

{
    "data": [
        {
            "id": "1407650287389712386",
            "value": "from:PreciseLabPL OR from:elonmusk"
        }
    ],
    "meta": {
        "sent": "2021-06-23T11:00:28.629Z"
    }
}
0reactions
gustawdanielcommented, Jun 23, 2021

There is connected issue:

https://github.com/twitterdev/Twitter-API-v2-sample-code/issues/34

I finally suggest write in README that this package can be used ONLY with node 12 until mentioned issue will be solved and that twitter developers are aware of this bug and works to fix it.


I confirming that

nvm use 12

fixed problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sampled stream - Handling disconnections - Twitter Developer
Every streaming connection is backed by a queue of messages to be sent to the client. If this queue grows too large over...
Read more >
Twitter's Broken Its Copyright Strike System, Users ... - Forbes
Last night, it became apparent that Twitter's automated copyright strike/takedown system was no longer functional.
Read more >
Twitter Sentiment Analysis: A tale of Stream Processing
Create a Twitter Developer account to access the API. In order to start collecting all those tweets, we will need a Twitter Developer ......
Read more >
Exclusive: Twitter is losing its most active users, internal ...
These "heavy tweeters" account for less than 10% of monthly overall users but generate 90% of all tweets and half of global revenue....
Read more >
13 Should We Use the Sample? Analyzing Datasets Sampled ...
Meanwhile, we gather the tweets of these users returned by the Twitter stream API at the same time period with two different access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found