Take the 2-minute tour ×

I'm trying to use the Net::Twitter::Stream Perl module from CPAN to read the stream from sample.json. I believe this is the corect module though they way they crafted it allows one to process the filter stream. I've modified it as such but I must be missing something as I don't get any data in return. I establish a connection but nothing comes back. I'm guessing this should be an easy fix but I'm a touch new to this part of Perl.....

package Net::Twitter::Stream;
use strict;
use warnings;
use IO::Socket;
use MIME::Base64;
use JSON;
use IO::Socket::SSL;
use LibNewsStand qw(%cf);
use utf8;

our $VERSION = '0.27';

=head1 NAME

Using Twitter streaming api. 


use Net::Twitter::Stream;

Net::Twitter::Stream->new ( user => $username, pass => $password,
                          callback => \&got_tweet,
                          track => 'perl,tinychat,emacs',
                          follow => '27712481,14252288,972651' );

 sub got_tweet {
 my ( $tweet, $json ) = @_;   # a hash containing the tweet
                                  # and the original json
 print "By: $tweet->{user}{screen_name}\n";
 print "Message: $tweet->{text}\n";


The Streaming verson of the Twitter API allows near-realtime access to
various subsets of Twitter public statuses.

The /1/status/filter.json api call can be use to track up to 200 keywords
and to follow 200 users.

HTTP Basic authentication is supported (no OAuth yet) so you will need
a twitter account to connect.

JSON format is only supported. Twitter may depreciate XML.

More details at: http://dev.twitter.com/pages/streaming_api

  user, pass: required, twitter account user/password
  callback: required, a subroutine called on each received tweet


=head1 UPDATES

https fix: iwan standley <iwan@slebog.net>


sub new {
  my $class = shift;
  my %args = @_;
  die "Usage: Net::Twitter::Stream->new ( user => 'user', pass => 'pass', 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

  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{track} && $args{follow};

  my $auth = encode_base64 ( "$args{user}:$args{pass}" );
  chomp $auth;

  my $cl = length $content;
  my $req = <<EOF;
GET /1/statuses/sample.json HTTP/1.1\r
Authorization: Basic $auth\r
Host: stream.twitter.com\r
User-Agent: net-twitter-stream/0.1\r
Content-Length: $cl\r

  my $sock = IO::Socket::INET->new ( PeerAddr => 'stream.twitter.com:https' );
  #$sock->print ( "$req$content" );
  while ( my $l = $sock->getline ) {
    last if $l =~ /^\s*$/;
    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};
share|improve this question

2 Answers 2

up vote 1 down vote accepted

You don't need to post the source, we can pretty much figure it out. You should try one of the examples, but my advice is to use AnyEvent::Twitter::Stream which comes with a good example that you only have to modify a bit to get it running

share|improve this answer
How do I modify this to get it working? I should simply just need to change my username and password, correct? I don't want to follow any users so I should be able to remove the user section and ensure the URL has 'sample' in it. I changed the 'warn' to print and I would think it would print out all tweets that come across. Still, I don't know how to engage it, it just dies after I run their first example. –  WildBill Nov 8 '11 at 0:33
Ok, got the example to work. Realized I have to call condvar form AnyEvent. Ex: my $done = AnyEvent->condvar; Must also call: $done->recv; Unsure why, will look into this but if anyone else knows why please contribute! –  WildBill Nov 9 '11 at 5:25
sub parse_from_twitter_stream {
  my $user = 'XXX';
  my $password = 'YYYY';

  my $stream = Net::Twitter::Stream->new ( user => $user, pass => $password,
                             callback => \&got_tweet,
                             connection_closed_cb => \&connection_closed,
                             track => SEARCH_TERM);

  sub connection_closed {
    sleep 1;
    warn "Connection to Twitter closed";
    parse_from_twitter_stream();#This isn't working for me -- can't get connection to reopen after disconnect

  sub got_tweet {
    #Do stuff here

share|improve this answer

Your Answer


