Streaming Callback

Oct 30, 2011 at 9:43 PM

Hi Joe,

I am looking at the Streaming API integration to Linq To Twitter.
Looking at some of the examples you have supplied, I can see that you use a callback function... the issues I am having is that the code inside of the callback function never seems to be executed... is this code running on a new thread? That makes sense I guess... should i somehow be marshalling the results back to the UI thread in order to see whats happening?

 

I am specifically interested in the Firehose Streaming API as I will need to make quite a few calls to the Twitter API if I am to acheive what I would like... I feel that the search api limits would not be sufficient!

 

 

Ben

Coordinator
Oct 30, 2011 at 9:58 PM

Hi Ben,

If you're attempting to use the Firehose, you won't see results yet because you need special permission from Twitter for that particular stream.  One approach you might want to take is to look at streams such as Filter or Sample that are open to everyone to get your basic logic in-place.  Then, when you get approval from Twitter for the Firehose, all you need to do is modify your query.

Yes, the callback runs on a new thread.  This means you'll need to marshal back onto the UI thread (i.e. Dispatcher.BeginInvoke for WPF or Silverlight).

@JoeMayo

Oct 30, 2011 at 10:16 PM

Hi Joe,

 

I have bascially created a really simple little app (a single windows form) with a button and a text box....

on the button click I have the following code....

 

        private void button1_Click(object sender, EventArgs e)
        {
            var auth = new AnonymousAuthorizer();
            int count = 0;
            using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/"))
            {
                twitterCtx.StreamingUserName = "myusername";
                twitterCtx.StreamingPassword = "mypassword";

                var streaming =
                    (from strm in twitterCtx.Streaming
                     where strm.Type == StreamingType.Filter &&
                           strm.Track == "#winning"
                     select strm)
                    .StreamingCallback(strm =>
                                           {
                                               this.textBox1.Text = strm.Content;

                        if (count++ >= 100)
                        {
                            strm.CloseStream();
                        }
                    })
                    .SingleOrDefault();
            }
        }

 

 

The problem is that as soon as I click the button... the form simply closes. No exceptions. No errors. Simply an exit code of 0 in the output window.
Any clues to what this might be?

 

Many Thanks

 

Ben

Oct 30, 2011 at 10:18 PM

Ps: Thanks for the awesome quick support!

Coordinator
Oct 30, 2011 at 10:36 PM

It sounds like you're experiencing an exception from either an application issue or from not marshalling the thread back to the UI.  Another source of exceptions is from various HTTP errors while communicating with Twitter.  Since you're using Windows Forms, you'll use a Control.Invoke to perform the marshalling:

http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

You can probably find plenty of examples of how to use this on the Internet.  The Silverlight Samples in the downloadable source code show how I used Dispatcher.Invoke with a lambda to keep the code inside of the callback, so there's multiple ways to do this.

For the exception handling, just in case there's an application error, you can wrap the whole query in a try/catch.  However, that isn't the only place an exception can occur and you might want to wrap the code inside of the callback lambda in a try/catch.

Essentially, you'll need to determine the cause of the problem to figure out what is happening and figure out a strategy.  Be sure to drill down into the exception in case it's being wrapped by something like a TargetInvocationException or to find more details if it's an HttpException.  Fiddler, F12 tools, or Firebug can help with HTTP errors.  One thing I would do is implement the marshalling of the callback onto the UI thread because that can definitely crash your application.

Joe