Skip to content

Instantly share code, notes, and snippets.

@robwormald
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robwormald/9317945 to your computer and use it in GitHub Desktop.
Save robwormald/9317945 to your computer and use it in GitHub Desktop.
var Twit = require('twit')
var twitterConnection = new Twit({
/**
* details go here
*/
})
var openStreams = {}
//url : what path to listen to
//event : stream event to listen for
//handler : what to do when event is fired
module.exports.listenToStream = function(url,event,handler){
//already an open listener
if(openStreams[url]) return openStreams[url]
var stream = twitterConnection.stream(url,options)
stream.on(event,handler)
openStreams[url] = stream;
return stream;
}
module.exports.closeStream = function(url){
if(openStreams[url]) openStreams[url].close()
}
module.exports.reopenStream = function(url){
if(openStreams[url]) openStreams[url].start()
}
//anywehre else
var statusStream = twitterService.listenToStream('/statuses','tweet',function(tweet){
//do something with a tweet
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment