Search code examples
xmlhttprequesttwittertitaniumdesktophttp-streaming

Architecture: Titanium Desktop against the Twitter Streaming API


I'm new to Titanium, and have started out by trying to build a (yet another) Twitter client. The problem I've encountered is that I would like to use Twitter's Streaming API, and I'm struggling to understand the best way to do that within Titanium Desktop.

Here's the options I see:

  1. Don't use the Streaming API, it's not going to work.
  2. Build a Python bridge that connects with a httpclient that supports streaming responses (required for the Streaming API, it never closes the connection). Let that client deliver the responses to a Javascript method that formats and outputs tweets as they come. (Problem here: How do I bundle the python libraries I need?)
  3. Use the Javascript HttpClient shipped with Titanium SDK 1.1 in some clever way I'm not aware of.
  4. Use the 1.2.0-RC2 release of Titanium SDK that ships with a HttpClient that has support for streaming responses. There's very little information in the Release notes to judge if the streaming support is enough to get things working with the Streaming API.
  5. Use twstreamer, a javascript library for streaming support through a Flash intermediary. I've seen bug reports stating the Flash does not work well inside Titanium Desktop, but I'd love to be proven wrong.
  6. Another way that I'm not yet thought of.

I'm hoping for all sorts of clever ideas of how I can get this working, and tips going forward. Thanks for reading!


Solution

  • Here's how to do it (after LOTS of testing):

    var xhr = Titanium.Network.createHTTPClient();
    xhr.open("GET", "https://stream.twitter.com/1/statuses/filter.json?track=<Your-keyword-to-track>", true, '<Your-twitter-nickname>', '<Your-twitter-password>');
    xhr.send();
    
    var last_index = 0;
    function parse() {
        var curr_index = xhr.responseText.length;
        if (last_index == curr_index) return; // No new data
        var s = xhr.responseText.substring(last_index, curr_index);
        last_index = curr_index;
        console.log(s);
    }
    
    var interval = setInterval(parse, 5000);
    setTimeout(function(){
        clearInterval(interval);
        parse();
        xhr.abort();
    }, 25000);