Documentation Index
Fetch the complete documentation index at: https://docs.x.com/llms.txt
Use this file to discover all available pages before exploring further.
title: “Streaming”
sidebarTitle: “Streaming”
description: The TypeScript SDK provides real-time streaming capabilities for live data feeds. Connect to real-time sampled posts: Consume the stream with async iter…---
The TypeScript SDK provides real-time streaming capabilities for live data feeds.
Basic Streaming
Connect to real-time sampled posts:
import { Client } from '@xdevplatform/xdk';
const client: Client = new Client({ bearerToken: 'your-bearer-token' });
// 1% sampled public posts
const stream = await client.stream.postsSample({
tweetFields: ['id','text','created_at'],
expansions: ['author_id'],
userFields: ['id','username','name']
});
// 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));
stream.on('close', () => console.log('Stream closed'));
Async Iteration
Consume the stream with async iteration:
const stream = await client.stream.postsSample();
for await (const event of stream) {
// Each event is a parsed JSON line (data/includes/matching_rules)
console.log(event);
}
Stream Management
Control lifecycle from the event-driven stream:
// Close the stream
stream.close();
// Auto-reconnect (if enabled by your wrapper)
// The default EventDrivenStream exposes basic reconnect hooks
Error Handling
Handle streaming errors and reconnections:
stream.on('error', (event) => {
const err = event.error || event;
console.error('Stream error:', err);
});
stream.on('keepAlive', () => {
// heartbeat event
});