-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathstream_posts_sample.js
More file actions
47 lines (40 loc) · 1.41 KB
/
stream_posts_sample.js
File metadata and controls
47 lines (40 loc) · 1.41 KB
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
// Open a live stream of roughly 1% random sample of publicly available posts
// https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/quick-start
const { Client } = require('@xdevplatform/xdk');
// 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 client = new Client({ bearerToken: token });
(async () => {
try {
// 1% sampled public posts
const stream = await client.stream.postsSample({
tweetFields: ['id', 'text', 'created_at']
});
// Listen to events
stream.on('data', (event) => {
// event is the parsed JSON line (data/includes/matching_rules)
console.log('New data:', event);
});
stream.on('error', (e) => {
console.error('Stream error:', e);
if (e.status === 401) {
console.log('Unauthorized');
process.exit(1);
} else if (e.detail === "This stream is currently at the maximum allowed connection limit.") {
console.log(e.detail);
process.exit(1);
}
});
stream.on('keepAlive', () => {
// heartbeat event - keep alive signal received
});
stream.on('close', () => {
console.log('Stream closed');
});
} catch (e) {
console.error('Error:', e);
process.exit(1);
}
})();