Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter streaming API example twitter-node and socket.io
<!DOCTYPE HTML>
<head>
<title>Codesnippit NodeJS Twitter Tracker Client</title>
</head>
<body>
<ul></ul>
<script>
(function() {
var script = document.createElement("script");
script.src = "http://code.jquery.com/jquery.min.js";
script.onload = function() {
$.getScript("http://localhost:3000/socket.io/socket.io.js", function() {
var socket = new io.Socket("localhost", {"port": 3000});
socket.on("message", function(json) {
data = JSON.parse(json);
$("<li></li>").text("@" + data.user.screen_name + " " + data.text).prependTo("ul");
});
socket.connect();
});
};
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>
var http = require('http');
var config = {
user: "",
password: "",
track: ["ipl"]},
sys = require('sys');
server = http.createServer(function(req, res) { // create the server
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Codesnippit Twitter Tracker Server is <span style="color: #bada55">operational</span></h1>');
});
server.listen(3000);
var socket = require('socket.io').listen(server),
twitter = new (require("twitter-node").TwitterNode)(config);
twitter
.addListener('error', function(error){ // Always check for errors or they popup client side
console.log(error.message);
})
.addListener('tweet', function(tweet){ // A new tweet that matches the criteria has been located
socket.broadcast(JSON.stringify(tweet));
})
.addListener('limit', function(limit){ // New limit has been sent from the API
sys.puts('LIMIT: ' + sys.inspect(limit));
})
.addListener('delete', function(del){ // A delete event occured
sys.puts('DELETE: ' + sys.inspect(del));
})
.addListener('end', function(resp){ // API disconnect
sys.puts('wave goodbye...' + resp.statusCode);
})
.stream();
@mrsteveheyes

This comment has been minimized.

Copy link

@mrsteveheyes mrsteveheyes commented Aug 26, 2011

Hi, I cam across this and it looks awesome. I tried to get it working and I came across this error :

TwitterNode parser error: Object #<Manager> has no method 'broadcast'

Any thoughts on what it could be?

Cheers,
Steve

@petermk

This comment has been minimized.

Copy link

@petermk petermk commented Mar 24, 2012

It's socket.emit, not socket.broadcast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment