# Stream record batches

With the [local Flight server](/arrow-flight-client/examples/local-server) running, call `doGet()` to consume batches as they arrive. The example prints the size of the first batch. Leaving a `for await` loop with `break` releases the stream.

`stream-batches.ts`

```ts
import { FlightClient } from 'arrow-flight-client';

async function main() {
  const client = new FlightClient('127.0.0.1:8815');

  try {
    for await (const flight of client.listFlights()) {
      const ticket = flight.endpoints.find(endpoint => endpoint.ticket)?.ticket;

      if (!ticket) {
        continue;
      }

      const reader = await client.doGet(ticket);

      for await (const chunk of reader) {
        if (chunk.data) {
          console.log('Batch rows:', chunk.data.numRows);
          break;
        }
      }

      return;
    }

    throw new Error('No Flight endpoint with a ticket was found');
  }
  finally {
    await client.close();
  }
}

await main();
```

Consume a `FlightStreamReader` with one `for await` loop or one `readAll()` call. Each stream item preserves the relationship between a `RecordBatch` and its application metadata. A server can also send metadata-only messages.

Start a new `doGet()` call with a valid ticket to read again. If the server supplied an `expirationTime`, the request can be retried before it expires. Otherwise, reuse follows the server's rules. [Flight contract](https://github.com/woodger/arrow-flight-client/blob/0.0.19/contracts/Flight.proto)

To inspect the schema only, open the stream and call `cancel()` after reading the schema.

`inspect-schema.ts`

```ts
import type { FlightClient, FlightTicket } from 'arrow-flight-client';

export async function inspectSchema(client: FlightClient, ticket: FlightTicket) {
  const reader = await client.doGet(ticket);

  try {
    console.log(reader.schema);
  }
  finally {
    await reader.cancel();
  }
}
```

`cancel()` aborts an active read with `AbortError` and resolves after the stream resources are released. Close the client with `client.close()` when all calls are done.
