Simple stream not stopping when asked

Jan 5, 2014 at 3:16 PM
Hello,

I managed to use streaminvi to get tweets from twitter, but something weird is happening.

What is the correct way to stop the stream?
I do it inside the Func code, and until this morning it worked great.
Suddenly the code doesn't stop anymore as if its in an infinite loop.
Tried following the streaminvi code, the problem is that when i do that it works, which means there is a synchronization problem somewhere in streaminvi code.
Well, its either that or i'm doing something really wrong.

Also, i used the StreamStopped event to shed some light on the matter, it returned null as a value.

linvi, can you provide an example of how it should be done? (already looked in the documentation, the example there is not stopping the stream, it runs forever).
Coordinator
Jan 5, 2014 at 8:39 PM
Hi there,

What is the correct way to stop the stream?
Normally you should always be able to stop a stream using Stream.StopStream().
SimpleStream stream = new SimpleStream("https://stream.twitter.com/1.1/statuses/sample.json");
stream.StartStream(token, tweet =>
{
          stream.StopStream();
});
This should stop the stream when you receive your first tweet.

Also, i used the StreamStopped event to shed some light on the matter, it returned null as a value.
The StreamStopped event returns the exception that occured to make the stream stop. Receiving null implies that the stream stopped normally, for example by invoking Stream.StopStream().

Please let me know if this help.
Linvi
Jan 6, 2014 at 6:04 AM
Edited Jan 6, 2014 at 6:08 AM
Hi linvi,

I tried your suggestion, it didn't work (the method never gets to the lines of code after this one).
this is my code:
_SimpleStream.StartStream(_Token, tweet =>
            {
                if (_Tweets.Count < iNumTweets)
                {
                    if (tweet.Hashtags.Count > 0 && tweet.Text.StartsWith("RT") 
                                                && tweet.Retweeting != null)
                    {
                        if (tweet.Retweeting.RetweetCount > 1)
                        {
                            _Tweets.Add(tweet.Retweeting);
                        }
                    }
                }
                else
                {
                    _SimpleStream.StopStream();
                }
            });
_Tweets is a member of the class.
_Token is a member of the class.
iNumTweets is the number of wanted tweets (consider it a constant).

Can you say if something is wrong with it ?
I'm using Tweetinvi 0.8.3.22

Thanks,

Yossi.
Coordinator
Jan 7, 2014 at 12:33 AM
Hi Yossi,

I am pretty confident with the fact that your condition _Tweets.Count < iNumTweets is never met because I believe that the condition tweet.Text.StartsWith("RT") is very rare and consequently you'll have to wait a long time before it is met.

Once the condition is met your stream should stop.

Here is a simpler example that displays the first 5 tweets received from the stream before stopping the stream.
The example also provides an example of the StreamStopped event and how you should expect it to behave
SimpleStream stream = new SimpleStream("https://stream.twitter.com/1.1/statuses/sample.json");

stream.StreamStopped += (sender, args) =>
{
    if (args.Value == null)
    {
        Console.WriteLine("Stream stopped correctly");
    }
    else
    {
        Console.WriteLine("An error occured with the message : {0}", args.Value.Message);
    }
};

var i = 0;
stream.StartStream(token, tweet =>
{
    Console.WriteLine(tweet.Text);

    ++i;
    if (i == 5)
    {
        stream.StopStream();
    }
});
Please let me know if this helps.
Linvi
Jan 7, 2014 at 5:57 AM
Edited Jan 7, 2014 at 11:36 AM
Hi Linvi,

First, i wish to thank you for your help.
To the point, i know for sure that the condition is met as i have debugged the code more than once.
You will be surprised to learn that tweet.Text.StartsWith("RT")
is not so rare and the condition _Tweets.Count < iNumTweets will be met in just 3 minutes (for iNumTweets = 100).

I urge you to test my code yourself to see that you have a bug on your hands.
I will try to debug the streaminvi code to see if i can put my finger on it.

Thanks,

Yossi.
Coordinator
Jan 7, 2014 at 12:51 PM
Hi Yossi,

I have tested your code and everything is working perfectly well for me and I couldn't find anything that would make your code fail in the source code.
Here is the test I have used.
SimpleStream stream = new SimpleStream("https://stream.twitter.com/1.1/statuses/sample.json");

var tweets = new List<ITweet>();
stream.StartStream(token, tweet =>
{
    if (tweets.Count < 10)
    {
        if (tweet.Hashtags.Count > 0 && tweet.Text.StartsWith("RT") && tweet.Retweeting != null)
        {
            if (tweet.Retweeting.RetweetCount > 1)
            {
                tweets.Add(tweet.Retweeting);
            }

            Console.WriteLine(tweet.Text);
        }
    }
    else
    {
        stream.StopStream();
    }
});
Please try this code as is. If it does not work for you you might have credentials issues.
If it does but your code still fails I will need have to have a look at your code.
You can contact me privately through the contact form of codeplex and I will be more than happy to debug this for you.

Linvi
Marked as answer by linvi on 1/16/2014 at 4:03 AM