How do you generate and post thousands of tweets in a week? One of my first projects at Engage was to scrape data and generate tweets for all new records fetched from an external API. Your first thought might be to store the data in a database and just remove or flag the record when it has been processed and the tweet has been posted. This might work depending on your setup, but it is not a good solution. A better solution is to use a work queue system, like Beanstalkd, to solve the problem.
Beanstalk is a simple, fast work queue. Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.
Lets use the Laravel framework and tools to quickly build something that works. Since Laravel comes with a built in queue component that handles multiple queueing services, including Beanstalkd, it’s easy to setup.
Use Composer to add the dependencies for the Beanstalkd PHP library:
To send the tweets I used the library twitter-api-php. Lets use Composer to add that to our dependencies too:
The Composer.json file should now contain something like this:
composer install or
composer update to make sure that everything is installed as it should.
Using Laravel together with Beanstalk and the twitter-api-php makes is really simple to queue and send tweets just when you want them posted.
First we need to setup a class to handle the jobs that we push on the queue. Create a file QueueTweet.php in your project with the following code:
You need to replace the strings in the
$settings array with your Twitter access tokens. You can create your own Twitter app and generate the access tokens needed here: apps.twitter.com.
Now we only need to push the messages on to the queue and Beanstalked will take care of the rest.
We use Laravel’s built in Queue component to post the job to Beanstalkd. Since I want the tweets posted spread out during the week I use the
Queue::later() function, which allows us to add a date and time for when the job should be processed. Carbon is a great library for handeling dates in PHP, and since Laravel already is dependent on it, we don’t need to import enything else to use it.
The only thing left to do now is to start Beanstalkd and a listener for the queue. We use Laravel’s artisan command to start the queue listener.
If everything works out as it should, you should see the tweets showing up on Twitter at the set date and time.
If you run into problems and want to inspect Beanstalkd’s queue you can use telnet to connect to the service and display stats for the different queues.
More commands for Beanstalkd can be found here.
A sample implementation can be found on Github