A simple example to create a websocket server and stream tweets about a pre-defined subject to the page.
$ npm install socket.io, twitter
$ node ./app.js
Then open http://localhost:1337/
in a browser that supports websockets (or iDevice).
var app = require('http').createServer(handler), | |
io = require('socket.io').listen(app), | |
fs = require('fs'), | |
sys = require('sys'), | |
twitter = require('twitter'); | |
app.listen(1337); | |
var twit = new twitter({ | |
consumer_key: 'CONSUMER_KEY', | |
consumer_secret: 'CONSUMER_SECRET', | |
access_token_key: 'ACCESS_TOKEN_KEY', | |
access_token_secret: 'ACCESS_TOKEN_SECRET' | |
}); | |
function handler (req, res) { | |
fs.readFile(__dirname + '/index.html', | |
function (err, data) { | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading index.html'); | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
} | |
var twee = io.of('tweet'); | |
twit.stream('statuses/filter', { track: 'javascript' }, function(stream) { | |
stream.on('data', function (data) { | |
io.sockets.emit('tweet', data.text); | |
console.log('.'); | |
}); | |
}); |
<!doctype html> | |
<title>Socket.io Demo</title> | |
<div id="tweets"></div> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect('/'), | |
tweets = document.getElementById('tweets'); | |
socket.on('tweet', function (data) { | |
tweets.innerHTML = tweets.innerHTML + '<br>' + data; | |
}); | |
</script> |