views:

109

answers:

1

Hi,

I'm thinking about using Rx (Reactive Framework) in order to asynchronously query Twitter search API on a given keyword. The sample app I'd like to build should be able to display new tweets in the console.

Do you think it is possible to do that ? Would it be simpler than using standard programming techniques ?

How would you do that ?

Thank ! Jeremy

+3  A: 

A quick mockup of how it could be done. Note, I've only done a simple web request but this should easily extend to interact with the Twitter API.

Update: my previous sample didn't work well with repeating requests. The following improved sample uses Observable.Interval to generate a continuous stream of ticks, driving the creation of requests and response download.

Observable
    .Interval(TimeSpan.FromSeconds(5))
    .Select(ticks => (HttpWebRequest)WebRequest.Create("http://demicode.com"))
    .Select(request => Observable.FromAsyncPattern(request.BeginGetResponse, 
        asyncResult => 
        {
            using(var response = request.EndGetResponse(asyncResult))
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                return DateTime.Now.ToString() + sr.ReadToEnd();
            }
        }))
    .SelectMany(getContent => getContent())
    .ObserveOnDispatcher()
    .Subscribe(content => downloadContent.Text = content);

Update 2: Seems like using libraries like TweetSharp would handle the Twitter requests nicely for you. Observable.FromAsyncPattern combined with the async twitter.BeginRequest method makes a nice combination.

Peter Lillevold
Thank you for the sample. Do you think it is possible to fetch each 5 seconds the new tweets ? As your example does not use Twitter, how would you do to check if the content of the page has changed in the last 5 seconds and show thoses changes ?
Jalfp
@Jalfp: see my improved sample. Still no Twitter integration though (I'll look into that if time permits), but the content download is at least downloading each 5 seconds.
Peter Lillevold
Thank you very much for your effort ! I'll try to use your sample for my idea. I really appreciate your help :-)
Jalfp
@Jalfp: how's the app going? added a tip to my answer if you haven't already found it.
Peter Lillevold