Skip to content

Instantly share code, notes, and snippets.

@mcroydon
Created August 25, 2010 13:43
Show Gist options
  • Save mcroydon/549532 to your computer and use it in GitHub Desktop.
Save mcroydon/549532 to your computer and use it in GitHub Desktop.

Adding streaming Twitter to the Socket.IO chat example

Below is a small diff showing how little it takes to hook the Twitter Streaming API in to the Socket.IO example using twitter-node, inspired by Cody Soyland's twirc.js.

To use, make sure you have twitter-node installed. If you have npm:

npm install twitter-node

If you're running the example directly from a git checkout of Socket.IO, make sure you get the client submodule first:

git submodule init
git submodule update

Here's an example of it in action:

An example of streaming twitter using Socket.IO and twitter-node.

diff --git a/example/server.js b/example/server.js
index 167fc43..db758a2 100644
--- a/example/server.js
+++ b/example/server.js
@@ -61,4 +61,16 @@ io.on('connection', function(client){
client.on('disconnect', function(){
client.broadcast({ announcement: client.sessionId + ' disconnected' });
});
-});
\ No newline at end of file
+});
+
+// Twitter bits
+var twitter = require('twitter-node');
+var twitterClient = new twitter.TwitterNode({
+ user: 'YOUR_USERNAME',
+ password: 'YOUR_PASSWORD',
+ track: ['nodejs', 'node.js', 'pants'],
+});
+twitterClient.addListener('tweet', function(tweet) {
+ console.log(tweet);
+ io.broadcast({message: ['@'+tweet.user.screen_name, tweet.text]});
+}).stream();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment