Adam Green
Twitter API Consultant

Three most important rules for running the streaming API framework

by Adam Green on January 31, 2014

in 140dev Source Code,Streaming API

I’m a typical programmer when it comes to reading documentation before I try to run someone else’s code. First I try to run the code, then I read the docs. Hey, if I needed clear documentation to get things working, would I have become a Twitter API programmer?

Unfortunately, Twitter’s streaming API requires a unique model of coding that is probably unlike any you’ve used before. Trust me, your first instincts will not work here. So I’ve compiled 3 basic rules that solve 90% of all support questions I receive when people first try to run my 140dev Streaming API Framework.

Rule #1: Do not run get_tweets.php as a cronjob
Cron is the backbone of Linux programming, so when I see a script that I need to run over a long period of time, I automatically create a cronjob for it. That is what many coders do when they start using my streaming API code. In the case of get_tweets.php, that will not work. This script must be run as a background process, which means it is started once and then run forever, or until you explicitly kill it. I do have a detailed install page that warns against this, but I wouldn’t read it myself. I’d just plunge ahead. So I understand why this is missed.

Here’s the problem, when you start get_tweets.php, it makes a connection to the streaming API on Twitter, and keeps it open. Tweets then start flowing into the database. It’s really cool. Where a cron job for this script causes problems, is that each time it runs another copy of get_tweets.php is started, even though the first one is still running. This results in additional connections to the streaming API being opened. Twitter hates that, and breaks the second connection. If your cron job keeps starting get_tweets.php over and over again, you can end up getting suspended, because Twitter thinks you are abusing its servers, which you are.

Rule #2: Read the install instructions and follow them line by line
I know you don’t want to do this. I wouldn’t want to do this either. But the reason I wrote this code and then documented it so thoroughly is that streaming API coding just isn’t like other PHP tasks.

Rule #3: Do not run get_tweets.php as a cronjob
Yeah, I’m being obnoxious here. My kids point this out all the time, but after explaining this issue countless times over the years, I know it is hard to absorb. I’m not being paid to do this. I want to help people get started with the streaming API. It is a totally amazing service that is completely free. I know that this is where people get caught when they start, and this is the best way I can think of to help them get started.

Leave a Comment

Previous post:

Next post: