> ## 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.

# 스트리밍

> 이벤트 리스너 또는 비동기 반복을 사용하여 TypeScript에서 X API 데이터를 실시간으로 스트리밍하고, 자동 재연결 및 스트림 라이프사이클 제어로 샘플 게시물을 처리하세요.

TypeScript SDK는 실시간 데이터 피드를 위한 스트리밍 기능을 제공합니다.

## 기본 스트리밍

실시간 샘플링된 게시물에 연결:

<CodeGroup>
  ```typescript title="stream.ts" lines wrap icon="square-js" theme={null} theme={null}
  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'));
  ```

  ```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>

## 비동기 반복

비동기 반복으로 스트림 사용:

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

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

## 스트림 관리

이벤트 기반 스트림의 라이프사이클 제어:

```typescript theme={null}
// Close the stream
stream.close();

// Auto-reconnect (if enabled by your wrapper)
// The default EventDrivenStream exposes basic reconnect hooks
```

## 오류 처리

스트리밍 오류 및 재연결 처리:

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

stream.on('keepAlive', () => {
  // heartbeat event
});
```

<Info>
  Javascript/TypeScript XDK를 사용하는 자세한 코드 예제는 [코드 샘플 GitHub 저장소](https://github.com/xdevplatform/samples/tree/main/javascript)를 확인하세요.
</Info>
