Building a Twitter Streaming Client with Node.js, Page 2
Building a Twitter Streaming Client
Building a simple Web server is an useful exercise for familiarizing oneself with Node syntax, however it's not a particularly compelling example of Node's true capabilities. So let's build something much more interesting which involves querying Twitter using the Twitter Search API.
This example works by periodically connecting to the Twitter API and searching the latest tweets for a specific keyword (which in the case of this example is defined by the variable keyword
).
Begin by loading the sys
and http
modules:
var sys = require('sys');
var http = require('http');
Next, define the server you'll be connecting to using the client, which in this case is Twitter's search API endpoint. You'll also define a variable which holds the keyword you'd like to search for, before creating the HTTP client connection using the http
module's createClient()
method:
var host = "search.twitter.com"
var keyword = "php";
var connection = http.createClient(80, host);
Next you'll define the function which will repeatedly execute in accordance with a specified event, which in the case of our example is the conclusion of a specified waiting period (timeout) of five seconds. This function will send the request to Twitter's API endpoint, subsequently receiving and parsing the search results before outputting select result fields to the caller:
function tweetStream() {
var request = connection.request('GET', "/search.json?q=" + keyword
+ "&since_id=" + counter
, {"host": host, "User-Agent": "TweetStreamer"});
request.addListener("response", function(response) {
var responseBody = "";
response.setEncoding("utf8");
response.addListener("data", function(chunk) { responseBody += chunk });
response.addListener("end", function() {
tweets = JSON.parse(responseBody);
var results = tweets["results"];
for(var x = 0; x < tweets["results"].length; x++) {
sys.puts("From: " + results[x].from_user + "n" + results[x].text + "nn");
}
});
});
request.end();
setTimeout(tweetStream, 5000);
};
Finally, you'll execute the function:
tweetStream();
Save this code to a script called something like tweet.js
and then execute it from the terminal like this:
$ node tweet.js
After a few moments you'll begin seeing the latest tweets containing the keyword mysql
scroll by, a few of which are presented here:
From: adeshosho
@pelosbriseno la verdad es que solo me faltan dos reportes, así que me las ingeniare con vb y mysql, ni hablar
From: EmpregaTI
RT @tramposTI: DBRINTERATIVA #sp seleciona PROGRAMADORES. ninjas de Web com domínio em php, ajax e mysql. cv para vagas@dbrinterativa.com
From: ratzpo
RT @skysql_ab: Former Oracle MySQL Customers Drive SkySQL Sales to Seven Figures in its First Twelve Weeks - http://bit.ly/i9rLr5
Learning Resources
- JSConf 2010 Node.js Presentation (PDF)
- JSConf EU 2009 Video Presentation: In this video Node.js project founder Ryan Dahl gives an excellent introduction to not only Node.js but the general challenges surrounding handling massive concurrent connections.
- TechCrunch Node.js Knockout Competition Favorite Apps: TechCrunch posted a list of their favorite Node.js Knockout contest entrants, a few of which are truly jaw-dropping.
Are you doing something cool with Node.js? Tell us about it in the comments!
About the Author
Jason Gilmore, Contributing Editor, PHP, is the founder of EasyPHPWebsites.com, and author of the popular book, "Easy PHP Websites with the Zend Framework". Jason is a cofounder and speaker chair of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer's conference, and was a member of the 2008 MySQL Conference speaker selection board.
Originally published on https://www.developer.com.
Page 2 of 2
This article was originally published on January 24, 2011