When starting user stream and network is unavailable, stream continuously raises Started and Stopped Events.

Feb 5, 2014 at 9:55 PM
Hi,

When I start a UserStream and I have no internet connectivity on my machine, I continuously receive alternating StreamStarted and StreamEnded events. Is this the desired behavior?

I would expect that I would not receive a Started or Stopped event until a stream was successfully initialized and started.

Thanks,
Coordinator
Feb 5, 2014 at 11:30 PM
Hi,

Thank you for reporting this issue, I will have a look tomorrow.
Is this issue happening on Tweetinvi 0.8.3.23 or Tweetinvi 0.9.

Linvi
Coordinator
Feb 6, 2014 at 12:04 PM
Hi,

Could you please send a code example, I am not able to reproduce this behavior.

Regards,
Linvi
Feb 6, 2014 at 2:28 PM
Hi,

I am currently using 0.8.3.23. I apologize I have incorrectly stated the issue. It occurs after a UserStream has successfully started. If I disconnect from the network and wait about 5 minutes, I repeatedly get alternating stopped and started events.

Along with every stopped event I receive:
System.NullReferenceException: Object reference not set to an instance of an object.
at Streaminvi.Helpers.StreamResultGenerator.StartStream(Func2 processObject, Func1 generateWebRequest)

The stopped event is expected as there is no network connection, but I don't think it should start again.

I noticed you added a StreamRunning event. I will try this out as it looks like it might solve my issue.

Thanks,
Coordinator
Feb 6, 2014 at 3:20 PM
Hi,

Thank for the details. I will attempt to reproduce your issue but could you please send me some code (privately if you do not wish to show it publicly) so that the reproduction is more accurate.

In the meantime, StreamRunning event is implement in Tweetinvi 0.9 and means that the Stream has concretely started to run and that any event related with the user will now be taken into consideration by the UserStream.

Linvi
Feb 6, 2014 at 5:00 PM
Hi,

So I've done some further investigation. It turns out that the repeated events are due to my retry logic that's described below. However I feel that there is still some unexpected behavior.

What I'm trying to do is simulate the internet connection going down my machine. If internet is down while I'm starting the stream I get a WebException on the token.ExceptionHandler, this is fine. However if the stream is successfully setup and the internet goes down (manually pulling the network cable) I would have expected a stream stopped. This doesn't happen right away, usually takes 5-10 minutes which is also fine. The stopped event is raise with:

Stopped, System.Net.WebException: The operation has timed out.
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()
at Streaminvi.Helpers.StreamResultGenerator.StartStream(Func2 processObject, Func1 generateWebRequest) in c:\Users\timart\Documents\My Box Files\Development\Tweetinvi\Release\Streaminvi\Helpers\StreamResultGenerator.cs:line 98

Now at this point I want to try to restart my stream and so I start the stream again (in the stopped event handler). The stream almost immediately fires another stopped event with:

Stopped, System.NullReferenceException: Object reference not set to an instance of an object.
at Streaminvi.Helpers.StreamResultGenerator.StartStream(Func2 processObject, Func1 generateWebRequest) in c:\Users\timart\Documents\My Box Files\Development\Tweetinvi\Release\Streaminvi\Helpers\StreamResultGenerator.cs:line 98

What I would expect is that on lost connection the stream is stopped. After which I can try to start the stream. The stream shouldn't raise started unless it is actually started.

Tweetinvi Version 0.8.3.23
Steps to Reproduce:
  1. Start the console app with internet connection
  2. Disconnect from internet
  3. Wait 5-10 minutes, stopped event will fire
  4. App will try to start stream once it's stopped, will receive StreamStarted followed by StreamStopped causing this step to be run in a loop
Console App:

class Program
{
private static IUserStream userStream;

static void Main(string[] args)
{

    string AccessToken = "AccessToken";
    string AccessTokenSecret = "AccessTokenSecret";
    string ConsumerKey = "ConsumerKey";
    string ConsumerKeySecret = "ConsumerKeySecret";

    var token = new Token(AccessToken, AccessTokenSecret, ConsumerKey, ConsumerKeySecret);
    token.ExceptionHandler += WebExceptionHandler;

    userStream = new UserStream(token);
    userStream.StreamStarted += us_StreamStarted;
    userStream.StreamStopped += us_StreamStopped;

    Task.Factory.StartNew(userStream.StartStream);
    Console.ReadLine();
}

private static void WebExceptionHandler(WebException webException)
{
    Debug.WriteLine("WebException, {0}", webException);
}

static void us_StreamStopped(object sender, TweetinCore.Events.GenericEventArgs<Exception> e)
{
    Debug.WriteLine("Stopped, {0}", e.Value);
    // Connection Lost Restart
    userStream.StartStream();
}

static void us_StreamStarted(object sender, EventArgs e)
{
    Debug.WriteLine("Started, {0}", e);
}
}

Let me know if anything is unclear,
Thanks,
Coordinator
Feb 6, 2014 at 5:20 PM
Hi,

Thanks this is way more clear, I think there is an example of such feature in Examplinvi but I will verify for UserStreams as they are behaving differently from all the other streams.
// Creating the stream and specifying the delegate
SimpleStream stream = new SimpleStream("https://stream.twitter.com/1.1/statuses/sample.json");

stream.StreamStopped += (sender, args) =>
{
    if (args.Value != null)
    {
        Console.WriteLine("An exception occured... Well just restart it!");
        Thread.Sleep(1000);
        stream.StartStream(token, x => ProcessTweet(x));
    }
};

// Starting the stream by specifying credentials thanks to the Token
stream.StartStream(token, x => ProcessTweet(x));
Also, if I solve this issue in Tweetinvi 0.9 would you be able to migrate or do you already have consequent quantity of code with 0.8.3?
Keep in touch.

Regards,
Linvi
Feb 6, 2014 at 6:18 PM
Migrating to Tweetinvi 0.9 will not be a problem.

Thanks for the example tip.

Regards,
kshan
Coordinator
Feb 7, 2014 at 10:57 AM
Hi,

Would you please try with Tweetinvi 0.9 as I do not have this issue.

Log:
Started
Runs
Stopped
END
Regards,
Linvi
Feb 10, 2014 at 3:01 PM
Hi,

This issue has been fixed with Tweetinvi version 0.9.

Thanks for your help,
kshan
Coordinator
Feb 10, 2014 at 7:46 PM
Thank you for reporting the issue.

Regards,
Linvi