Sunday 15 April 2012

twitter - how to add to add socket.io subscribers in node.js without rerunning setup code -



twitter - how to add to add socket.io subscribers in node.js without rerunning setup code -

now edited solution

i'm writing web page, backed node , express, uses twitter streaming api plot location of tweets containing given word onto google map. page connects server , places each tweet location server emits onto map.

the node js application this. twitfactory gives me twitter object twitter npm module various connection strings.

'use strict'; var express = require('express'); var app = express(); var server = require('http').server(app); var io = require('socket.io')(server); var twitfactory = require('./lib/twitfactory'); var twit = twitfactory.twit(); var searchterm = process.argv[2]; var locations = '-180,-90,180,90'; server.listen(3000); app.use('/bower_components', express.static(__dirname + '/bower_components')); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function (socket) { twit.stream('filter', {locations: locations}, function (stream) { stream.on('data', function (tweet) { if (tweet.text && tweet.text.tolowercase().indexof(searchterm.tolowercase()) != -1) { if (tweet.text && tweet.coordinates && tweet.coordinates.coordinates && tweet.coordinates.type === 'point') { socket.emit('tweet', { text: tweet.text, lon: tweet.coordinates.coordinates[0], lat: tweet.coordinates.coordinates[1] }); } } }); }); });

i have working single web client, fine development environment, want avoid re-initialising twitter connection within on connection event every time new client connects, or nail api phone call limit.

i think want define twitter stream separately, , add together new connections socket's subscribers, can't work out how code this, without nested construction see above.

(i realise it's inefficient tweets location filter content, few tweets have location attached, , api not allow specify location , search term, gives tweets either.)

update:

i found solution utilize different twitter node module allows stream object , events declared separately. it's twit (as opposed twitter) in npm. code looks this:

'use strict'; var express = require('express'); var app = express(); var server = require('http').server(app); var io = require('socket.io')(server); var logger = require('./lib/logger'); var twitfactory = require('./lib/twitfactory'); var searchterm = process.argv[2]; var locations = ['-180','-90','180','90']; var twit = twitfactory.twit(); var tweetstream = twit.stream('statuses/filter', {locations: locations}); server.listen(3000); app.use('/bower_components', express.static(__dirname + '/bower_components')); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function (socket) { console.log('new connection'); tweetstream.on('tweet', function (tweet) { if (tweet.text && tweet.text.tolowercase().indexof(searchterm.tolowercase()) != -1) { if (tweet.text && tweet.coordinates && tweet.coordinates.coordinates && tweet.coordinates.type === 'point') { socket.emit('tweet', { text: tweet.text, lon: tweet.coordinates.coordinates[0], lat: tweet.coordinates.coordinates[1] }); } } }); });

node.js twitter socket.io

No comments:

Post a Comment