Hi monks,

I'm using Net::Twitter::Stream to connect to the Twitter Streaming API (as Miyagawa's related module, AnyEvent::Twitter::Stream, seems to be out of commission). I'm running this process in the background using nohup. However, the disconnect callback code (shown below) doesn't seem to be reconnecting the socket upon API disconnect (everything works fine for a half hour, or more...but then stops updating). Can anyone see any flaws in the logic of this code? I'm not extremely well-versed at socket programming.

Below is how I'm calling it -- it seems as though the "connection_closed_cb" arg and corresponding callback do not properly handle disconnection from the API:

use constant SEARCH_TERM => "some_search_term"; parse_from_twitter_stream(); sub parse_from_twitter_stream { my $user = 'XXXXX'; my $password = 'YYYYY'; my $stream = Net::Twitter::Stream->new ( user => $user, pass => $pas +sword, callback => \&got_tweet, connection_closed_cb => \&connection_clos +ed, track => SEARCH_TERM); sub connection_closed { sleep 1; warn "Connection to Twitter closed"; parse_from_twitter_stream(); } sub got_tweet { my ( $tweet, $json ) = @_; # a hash containing the tweet # and the original json warn "Got tweet"; #I have more code here but it's snipped } }

This is the code from Net::Twitter::Stream -- see especially the last line of code. Not entirely sure what that does:

sub new { my $class = shift; my %args = @_; die "Usage: Net::Twitter::Stream->new ( user => 'user', pass => 'pas +s', callback => \&got_tweet_cb )" unless $args{user} && $args{pass} && $args{callback}; my $self = bless {}; $self->{user} = $args{user}; $self->{pass} = $args{pass}; $self->{got_tweet} = $args{callback}; $self->{connection_closed} = $args{connection_closed_cb} if $args{connection_closed_cb}; my $content = "follow=$args{follow}" if $args{follow}; $content = "track=$args{track}" if $args{track}; $content = "follow=$args{follow}&track=$args{track}\r\n" if $args{tr +ack} && $args{follow}; my $auth = encode_base64 ( "$args{user}:$args{pass}" ); chomp $auth; my $cl = length $content; my $req = <<EOF; POST /1/statuses/filter.json HTTP/1.1\r Authorization: Basic $auth\r Host: stream.twitter.com\r User-Agent: net-twitter-stream/0.1\r Content-Type: application/x-www-form-urlencoded\r Content-Length: $cl\r \r EOF my $sock = IO::Socket::SSL->new ( PeerAddr => 'stream.twitter.com:ht +tps' ); $sock->print ( "$req$content" ); while ( my $l = $sock->getline ) { last if $l =~ /^\s*$/; } while ( my $l = $sock->getline ) { next if $l =~ /^\s*$/; # skip empty lines $l =~ s/[^a-fA-F0-9]//g; # stop hex from compaining about +\r my $jsonlen = hex ( $l ); last if $jsonlen == 0; eval { my $json; my $len = $sock->read ( $json, $jsonlen ); my $o = from_json ( $json ); $self->{got_tweet} ( $o, $json ); }; } $self->{connection_closed} ( $sock ) if $self->{connection_closed}; }
Any help is much appreciated. Thanks!!

In reply to Net::Twitter::Stream socket issue by sethviebrock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.