メインコンテンツへスキップ
TypeScript SDK は、ライブデータフィード向けのリアルタイムストリーミング機能を提供します。

基本的なストリーミング

リアルタイムのサンプル投稿に接続する:
import { Client } from '@xdevplatform/xdk';

const client: Client = new Client({ bearerToken: 'your-bearer-token' });

// 公開投稿の1%サンプル
const stream = await client.stream.postsSample({
  tweetfields: ['id','text','created_at'],
  expansions: ['author_id'],
  userfields: ['id','username','name']
});

// イベントを待ち受け
stream.on('data', (event) => {
  // event はパース済みの JSON 行(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'));

非同期イテレーション

非同期イテレーションでストリームを処理します:
const stream = await client.stream.postsSample();
for await (const event of stream) {
  // 各イベントはパース済みのJSON行(data/includes/matching_rules)です
  console.log(event);
}

ストリーム管理

イベント駆動型ストリームからライフサイクルを管理します:
// ストリームを閉じる
stream.close();

// 自動再接続(ラッパーで有効にしている場合)
// デフォルトのEventDrivenStreamは基本的な再接続フックを提供します

エラー処理

ストリーミング中のエラーと再接続を適切に処理します。
stream.on('error', (event) => {
  const err = event.error || event;
  console.error('ストリームエラー:', err);
});

stream.on('keepAlive', () => {
  // キープアライブイベント
});