forked from xdevplatform/Twitter-API-v2-sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampled_stream.js
60 lines (53 loc) · 1.89 KB
/
sampled_stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Open a live stream of roughly 1% random sample of publicly available Tweets
// https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/quick-start
const needle = require('needle');
// The code below sets the bearer token from your environment variables
// To set environment variables on macOS or Linux, run the export command below from the terminal:
// export BEARER_TOKEN='YOUR-TOKEN'
const token = process.env.BEARER_TOKEN;
const streamURL = 'https://api.twitter.com/2/tweets/sample/stream';
function streamConnect(retryAttempt) {
const stream = needle.get(streamURL, {
headers: {
"User-Agent": "v2SampleStreamJS",
"Authorization": `Bearer ${token}`
},
timeout: 20000
});
stream.on('data', data => {
try {
const json = JSON.parse(data);
console.log(json);
// A successful connection resets retry count.
retryAttempt = 0;
} catch (e) {
// Catches error in case of 401 unauthorized error status.
if (data.status === 401) {
console.log(data);
process.exit(1);
} else if (data.detail === "This stream is currently at the maximum allowed connection limit.") {
console.log(data.detail)
process.exit(1)
} else {
// Keep alive signal received. Do nothing.
}
}
}).on('err', error => {
if (error.code !== 'ECONNRESET') {
console.log(error.code);
process.exit(1);
} else {
// This reconnection logic will attempt to reconnect when a disconnection is detected.
// To avoid rate limits, this logic implements exponential backoff, so the wait time
// will increase if the client cannot reconnect to the stream.
setTimeout(() => {
console.warn("A connection error occurred. Reconnecting...")
streamConnect(++retryAttempt);
}, 2 ** retryAttempt);
}
});
return stream;
}
(async () => {
streamConnect(0)
})();