# Tweetstream

**URL:** <https://www.ruby-forum.com/t/tweetstream/184392>\
**Category:** Rails\
**Created:** [March 1, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/tweetstream/184392 "2010-03-01T21:51:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [March 1, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/tweetstream/184392/1 "2010-03-01T21:51:39Z")

</div>

Hi, I want to use the TweetStream gem  
[http://github.com/intridea/tweetstreamin](http://github.com/intridea/tweetstreamin) my Rails app

I can’t seem to figure out how to do it, because it opens a connection  
and  
then won’t move on.

I got it to work in a Ruby script with

---------- start code ----------  
require ‘rubygems’  
require ‘tweetstream’

user = ‘blinkdev’  
password = **secret**  
keyword = ‘#ROCKPIC’

Thread.new do  
TweetStream::Client.new(user,password).track(keyword) do |status|  
puts “[#{status.created\_at}-#{status.user.screen\_name}]  
#{status.text}”  
end  
end

loop do  
sleep 10  
puts “other stuff happening”  
end

---------- end code ----------

Which gives the output:  
other stuff happening  
other stuff happening  
[Mon Mar 01 20:30:59 +0000 2010-blinkdev] test #ROCKPIC 7  
other stuff happening

So I tried to replicate that in my Rails app in the file  
config/initializers/twitter\_stream.rb  
---------- start code ----------  
require ‘rubygems’  
require ‘tweetstream’

Thread.new do  
logger.info “started tweetstream”

TweetStream::Client.new(‘blinkdev’, **secret** ).track(‘#ROCKPIC’) do  
|status|  
message = “[#{status.created\_at}-#{status.user.screen\_name}]  
#{status.text}”  
logger.info message  
logger.info Rockpic.new( :message =\> message ).save!  
end

end  
---------- end code ----------

But it didn’t work. I couldn’t get it to log anything either. So I went  
and  
tried it in the console:

---------- start code ----------  
$script/console  
Loading development environment (Rails 2.3.3)

> > require ‘rubygems’  
> > =\> []  
> > require ‘tweetstream’  
> > =\> []

?\> Thread.new do  
?\> logger.info “started tweetstream”

> >

?\> TweetStream::Client.new(‘blinkdev’, **secret** ).track(‘#ROCKPIC’) do  
|status|  
?\> message = “[#{status.created\_at}-#{status.user.screen\_name}]  
#{status.text}”

> > ```
> > logger.info message
> > logger.info Rockpic.new( :message => message ).save!
> > 
> > ```
> > 
> > end

?\> end  
=\> #\<Thread:0x24ba60c dead\>  
---------- end code ----------

It doesn’t seem to do anything. I tried playing with daemons, but I  
can’t  
figure out how to use them either.  
If someone can show me how to use daemons, I’ll give that a try (I have  
the  
daemons gem installed [daemons | RubyGems.org | your community gem host](http://rubygems.org/gems/daemons)) and it looks  
like  
TweetStream has their own version with Daemons,  
[http://rdoc.info/rdoc/intridea/tweetstream/blob/e5ecb9ef125ebce038d24f816973bbb4644445a8/TweetStream/Daemon.htmlbut](http://rdoc.info/rdoc/intridea/tweetstream/blob/e5ecb9ef125ebce038d24f816973bbb4644445a8/TweetStream/Daemon.htmlbut)  
I can’t seem to get it to do anything:

---------- start code ----------  
ARGV \<\< ‘start’  
require ‘rubygems’  
require ‘tweetstream’

user = ‘blinkdev’  
password = **secret**  
keyword = ‘#ROCKPIC’

TweetStream::Daemon.new(user,password, ‘tracker’).track(keyword) do  
|status|  
puts “[#{status.created\_at}-#{status.user.screen\_name}]  
#{status.text}”  
end  
---------- end code ----------

It runs, but it doesn’t respond to anything, and I think I need it to  
run in  
the Rails environment, because I want to host it on Heroku. Anyway, I’m  
not  
sure where to go with it, I just want my Rails app to have a stream to  
twitter to watch for a given hash tag, and a particular user’s tweets.

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [March 2, 2010, 1:42am UTC](https://www.ruby-forum.com/t/tweetstream/184392/2 "2010-03-02T01:42:16Z")

</div>

Based on their example, I got this to work:  
---------- start code ----------  
require ‘rubygems’  
require ‘tweetstream’  
require ‘ruby-growl’

username = ‘blinkdev’  
password = **secret**  
tracks = [’#ROCKPIC’]  
puts “Starting a GrowlTweet to track: #{tracks.inspect}”

TweetStream::Daemon.new(username,password).track(\*tracks) do |status|  
g = Growl.new ‘localhost’, ‘growltweet’, [‘tweet’]  
g.notify ‘tweet’, status.user.screen\_name, status.text  
end  
---------- end code ----------

And run it from the command line like  
$ruby test.rb start  
$ruby test.rb stop

So I want to pull that into my rails app, I wrote this file to load the  
environment, and save the statuses  
---------- start code ----------

# file: RAILS\_ROOT/jobs/twitter\_stream.rb

ENV[‘RAILS\_ENV’] = ARGV.shift  
require File.expand\_path(File.dirname( **FILE** ) +  
“/…/config/environment”)

require ‘rubygems’  
require ‘tweetstream’

username = ‘blinkdev’  
password = **secret**  
tracks = [’#ROCKPIC’]

TweetStream::Daemon.new(username,password).track(\*tracks) do |status|  
Rockpic.new( :message =\>  
“[#{status.created\_at}-#{status.user.screen\_name}] #{status.text}”  
).save!  
end  
---------- end code ----------

and then to load it, I created

---------- start code ----------

# file: RAILS\_ROOT/config/initializers/twitter\_stream.rb

puts ‘pre’  
system “#{%x(which ruby).chomp}  
#{File.join(RAILS\_ROOT,‘jobs’,‘twitter\_stream.rb’)} #{ENV[‘RAILS\_ENV’]}  
restart”  
puts ‘post’  
---------- end code ----------

But it never finishes that command, when I start my server it just keeps  
writing ‘pre’ over and over, implying to me that it is recursively  
calling  
itself. ie the initializer starts the daemon, which requires the  
environment, which runs the initializer, which starts the daemon, etc.

I figure if I can get it to realize that it is already running, then I  
should be good, but I’m not very experienced with this, and don’t know  
how  
to check for that. Api doesn’t look like it offers a way, but looking at  
the  
code shows that it uses Daemons underneath of it. Not sure how to do it,  
still, though. Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [March 2, 2010, 1:44am UTC](https://www.ruby-forum.com/t/tweetstream/184392/3 "2010-03-02T01:44:36Z")

</div>

Hi Josh,

If your overall goal is to run it as a background task on Heroku, I  
think you want this: [http://docs.heroku.com/background-jobs](http://docs.heroku.com/background-jobs).

I don’t think that there’s any other way to run background jobs on  
Heroku.

Regards  
Ritchie

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [March 2, 2010, 1:56am UTC](https://www.ruby-forum.com/t/tweetstream/184392/4 "2010-03-02T01:56:18Z")

</div>

On Mon, Mar 1, 2010 at 6:43 PM, Ritchie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Josh,
> 
> If your overall goal is to run it as a background task on Heroku, I  
> think you want this: [http://docs.heroku.com/background-jobs](http://docs.heroku.com/background-jobs).
> 
> I don’t think that there’s any other way to run background jobs on  
> Heroku.
> 
> Regards  
> Ritchie

Thanks, Ritchie, I figured the TweetStream should be fine since it runs  
on  
Daemons, which is listed in heroku’s installed gems at  
[http://installed-gems.heroku.com](http://installed-gems.heroku.com)  
But I’ll read through that and see if a background task makes more  
sense, I  
appreciate it.
