> ## Documentation Index
> Fetch the complete documentation index at: https://generaltranslation.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ストリーミング

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

<div id="basic-streaming">
  ## 基本的なストリーミング
</div>

リアルタイムのサンプル投稿ストリームに接続する:

<CodeGroup>
  ```typescript stream.ts theme={null} theme={null}
  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'));
  ```

  ```javascript stream.js theme={null} theme={null}
  import { Client } from '@xdevplatform/xdk';

  const client = new Client({ bearerToken: 'your-bearer-token' });
  const stream = await client.stream.postsSample({ tweetFields: ['id','text'] });

  stream.on('data', (event) => {
    console.log('New data:', event);
  });
  stream.on('error', (e) => console.error('Stream error:', e));
  stream.on('close', () => console.log('Stream closed'));
  ```
</CodeGroup>

<div id="async-iteration">
  ## 非同期イテレーション
</div>

非同期イテレーションでストリームを処理します:

<CodeGroup>
  ```typescript async.ts theme={null} theme={null}
  const stream = await client.stream.postsSample();
  for await (const event of stream) {
    // 各イベントは解析済みの JSON 行（data/includes/matching_rules）です
    console.log(event);
  }
  ```

  ```javascript async.js theme={null} theme={null}
  const stream = await client.stream.postsSample();
  for await (const event of stream) {
    console.log(event);
  }
  ```
</CodeGroup>

<div id="stream-management">
  ## ストリーム管理
</div>

イベント駆動ストリーム側からライフサイクルを制御する:

```typescript theme={null}
// ストリームを閉じる
stream.close();

// 自動再接続（ラッパーで有効にしている場合）
// デフォルトのEventDrivenStreamは基本的な再接続フックを公開する
```

<div id="error-handling">
  ## エラー処理
</div>

ストリーミング中のerrorsと再接続を処理します。

```typescript theme={null}
stream.on('error', (event) => {
  const err = event.error || event;
  console.error('Stream error:', err);
});

stream.on('keepAlive', () => {
  // ハートビートイベント
});
```

<Info>
  JavaScript/TypeScript 向け XDK を使用した詳細なコードサンプルについては、[code samples GitHub リポジトリ](https://github.com/xdevplatform/samples/tree/main/javascript)をご覧ください。
</Info>
