const bearerToken = 'xxxxxxxxxxx';
const url = 'https://api.twitter.com/2/tweets/search/stream?tweet.fields=created_at&expansions=author_id&user.fields=name,username,profile_image_url';
const configuration = {
timeout: 30000,
retry: {
base: 10000,
customBackoff: (backoff) => (
backoff + 5000
)
},
};
const ts = new TwitterStreams(bearerToken, url, configuration);
(async () => {
await ts.createRules('googledown');
const stream = await ts.createConnection();
ts.processStream(stream, async (tweet) => {
// your tweet processing logic here
});
})();const ts = new TwitterStreams(bearerToken, url, configuration);bearerToken: Twitter Api authentication tokenurl: Twitter stream urlconfiguration: TwitterStreams configuration
const configuration = {
timeout: 30000,
retry: {
base: 10000,
customBackoff: (backoff) => (
backoff + 5000
)
},
logLevel: 'info'
}timeout: maximum time between two stream data points. If timeout is exceeded the connection will be considered stale. Default: 20000 ms.retry: retry options. If is set the retry mechanism will intervene in case of disconnection or stale connection.retrycan be a boolean value or an object containing custom options.base: backoff time to wait before retrying the connection. Default: 15000 ms.customBackoff: custom backoff algorithm to manage the retry mechanism. Default: exponential.logLevel: Stream log level. Default: info.
Create a stream connection to the specified url in the TwitterStreams constructor.
const ts = new TwitterStreams(bearerToken, url, configuration);
const stream = await ts.createConnection();Use a stream connection to process data in a stream
const ts = new TwitterStreams(bearerToken, url, configuration);
const stream = await ts.createConnection();
ts.processStream(stream);You can optionally give, as callback to processStream, a custom function to process tweet data.
Your custom function must accept only one argument, which is the tweet parsed JSON object.
ts.processStream(stream, async (tweet) => {
console.log(tweet);
// your tweet processing logic here
});Create Twitter stream rules on specific hashtag.
Use this the first time you use TwitterStream.
const ts = new TwitterStreams(bearerToken, url, configuration);
await ts.createRules('myhashtag');Get active Twitter stream rules
await ts.getRules();Delete Twitter stream rules on a specific hashtag.
await ts.deleteRules('myhashtag');Overwrite Twitter stream rules
Use this when you want to change hashtag.
await ts.overwriteRules('myhashtag');