[Streaming API] How to properly reconnect

May 29, 2015 at 7:25 PM
Hello

Do you have any best practices for [Streaming API] reconnect strategy in case of any network problems? Any code examples please.

Regards
Ozzy
Coordinator
May 29, 2015 at 9:50 PM
Oh,

You start asking confidential questions here.
Their are many many ways to implement a reconnect strategy. I have been discussing this over and over.

To make it simple, Tweetinvi is already attempting to reconnect in case of simple failure (very short drop of the connection).
If Tweetinvi cannot handle a drop of connection or any other issue that might occur (believe me there are a lot of scenarios) you can simply listen to the StreamStopped event.

Let me know if the following code helps you enough to solve your problem.
var stream = Stream.CreateSampleStream();
stream.StreamStopped += (sender, args) =>
{
    if (args.Exception != null)
    {
        Thread.Sleep(2000);
        stream.StartStream();
    }
};
stream.StartStream();
Linvi
Marked as answer by linvi on 6/1/2015 at 1:42 AM
Jun 1, 2015 at 8:31 PM
It works thanks!

Example for Filtered Stream that works for me:
            stream = Stream.CreateFilteredStream();

            stream.AddFollow(759251); //CNN

            stream.MatchingTweetReceived += (sender, args) =>
            {
                var tweet = args.Tweet;

                // To do something
            };

            // It works as retry logic
            stream.StreamStopped += (sender, args) =>
            {
                if (args.Exception != null)
                {
                    Thread.Sleep(5000);
                    stream.StartStreamMatchingAnyCondition();
                }
            };

            stream.StartStreamMatchingAnyCondition();
Marked as answer by Gravitcapa on 6/1/2015 at 12:32 PM