Twitter Streaming APIs with Ruby

1. Introduce about Twitter Streaming APIs

Twitter Streaming APIs provide programmatic access to monitor or process Tweets in real-time.

Twitter offers several streaming endpoints:

  • Public streams: Streams of the public data flowing through Twitter. Suitable for following specific users or topics, and data mining.
  • User streams: Single-user streams, containing roughly all of the data corresponding with a single user’s view of Twitter.
  • Site streams: The multi-user version of user streams. Site streams are intended for servers which must connect to Twitter on behalf of many users.

2. How to get Twitter API Keys

Step 1: Access to

Step 2: Click to "Create New App" to create your application.

Step 3: After creating, click to "Keys and Access Tokens" to get API Key and API secret.

Step 4: Sroll down to "Your Access Token" section, click to "Create my access token" to get Access Token and Access Token Secret.  

3. Get Data from Twitter Streaming APIs in Ruby

We use twitter gem and API Keys to connect to Twitter Streaming API.

3.1 Installation

gem install twitter


3.2 Configuration for Streaming

client = do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"


3.3 Usage

* Stream tweets mentions of ruby or python:

topics = ["ruby", "python"]
client.filter(track: topics.join(",")) do |tweet|
  puts tweet.text

* Stream tweets from San Francisco and language is en:

@client.filter({locations: "-122.75,36.8,-121.75,37.8", language: "en"}) do |tweet|
  puts tweet.text

Tip: Sometimes you get the error: too many request and how to handle rate limits:

  @client.filter({locations: "-122.75,36.8,-121.75,37.8", language: "en"}) do |tweet|
    puts object.text
rescue Twitter::Error::TooManyRequests => error
  # NOTE: Your process could go to sleep for up to 15 minutes but if you
  # retry any sooner, it will almost certainly fail with the same exception.
  sleep error.rate_limit.reset_in + 1

4. References