Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

README.md

216 lines8.6 KB

1# TanStack AI React Native Chat
2​
3Manual React Native verifier for TanStack AI streaming transports, presented as
4a multi-turn mobile recipe app. If you want the docs-first setup path, start
5with the public [Quick Start: React Native](https://tanstack.com/ai/latest/docs/getting-started/quick-start-react-native).
6​
7This example targets Expo SDK 54 so it can be opened with the Expo Go app
8currently distributed through the Play Store for SDK 54.
9​
10You start with an Expo app, a local Hono/OpenAI server, and an Expo Go device or
11emulator. By the end of this README, you can scan the QR code, pick a transport,
12and stream structured recipe revisions from OpenAI without bundling provider
13secrets into the app.
14​
15This Expo app talks to a local Hono server and lets you manually test:
16​
17- `fetchHttpStream` against `POST /chat/http`
18- `xhrHttpStream` against `POST /chat/http`
19- `xhrServerSentEvents` against `POST /chat/sse`
20​
21The UI behaves like a recipe app first and a transport tester second. It keeps
22transport controls in a compact Testing mode panel, then shows the latest
23recipe as the primary card with title, summary, servings, timing, tags,
24ingredients, steps, notes, warnings, and revision. Compact request bubbles
25remain available so you can confirm each follow-up changed the recipe.
26​
27The server streams live structured recipe responses from OpenAI through
28`@tanstack/ai` and `@tanstack/ai-openai`. It is OpenAI-only and does not include
29deterministic recipe fixtures or a no-key fallback.
30​
31Create `examples/ts-react-native-chat/.env` or set shell environment variables
32before running the example. Values loaded from `.env` are server-only by
33default; the dev runner passes them to the local Hono server, but does not pass
34non-public values such as `OPENAI_API_KEY` to Expo or the phone:
35​
36- `OPENAI_API_KEY` is required for `/chat/http` and `/chat/sse`.
37- `OPENAI_MODEL` is optional and defaults to `gpt-5.2`.
38​
39`.env` example:
40​
41```env
42OPENAI_API_KEY=sk-...
43OPENAI_MODEL=gpt-5.2
44```
45​
46PowerShell example if you prefer shell variables:
47​
48```powershell
49$env:OPENAI_API_KEY = 'sk-...'
50$env:OPENAI_MODEL = 'gpt-5.2'
51pnpm --filter ts-react-native-chat dev
52```
53​
54The `dev` and `dev:server` scripts load `.env` from this example directory for
55the server process. The `dev` script starts Expo with Expo dotenv loading
56disabled and only passes public/mobile configuration, such as
57`EXPO_PUBLIC_TANSTACK_AI_BASE_URL`, so secrets are not bundled into the native
58app. If `OPENAI_API_KEY`, `OPENAI_MODEL`, or another server setup value is
59invalid, the app receives a stream error with an actionable message instead of a
60bare XHR `400 undefined` failure.
61​
62The package scripts that invoke Expo directly, such as `dev:app` and
63`smoke:expo`, also start Expo with dotenv loading disabled. If you run ad hoc
64Expo commands manually from this example, such as
65`pnpm --filter ts-react-native-chat exec expo install --check`, set
66`EXPO_NO_DOTENV=1` first when `.env` contains provider credentials.
67​
68## Run
69​
70```bash
71pnpm install --no-frozen-lockfile
72pnpm --filter ts-react-native-chat dev
73```
74​
75The `dev` script starts both processes:
76​
77- Hono server on `0.0.0.0:8787` by default
78- Expo/Metro dev server for the native React Native app in LAN mode
79​
80`pnpm dev` prints an Expo Go QR code. Scan it from a phone on the same Wi-Fi
81network to open the app.
82​
83The script also auto-detects a non-internal IPv4 LAN address and starts Expo
84with `EXPO_PUBLIC_TANSTACK_AI_BASE_URL=http://<lan-ip>:8787`, so the phone can
85reach the Hono server. If you already set `EXPO_PUBLIC_TANSTACK_AI_BASE_URL`,
86the script keeps your value.
87​
88Only `EXPO_PUBLIC_*` values are intended to reach the React Native app and
89phone. Keep provider credentials and other server-only settings as plain
90non-public names, such as `OPENAI_API_KEY`, in `.env`.
91​
92Expo manages the Metro host for the QR code, manifest, and bundle URLs. The
93script does not set `REACT_NATIVE_PACKAGER_HOSTNAME` unless you already set it
94in your environment.
95​
96Set `PORT` if you want the Hono server to listen somewhere else. `pnpm dev`
97uses the same port for the server process and the generated Expo backend URL.
98​
99To inspect the chosen addresses without starting dev servers:
100​
101```bash
102pnpm --filter ts-react-native-chat dev -- --print-config
103```
104​
105The config includes `lanInterfaceName` so you can confirm the selected backend
106IP came from a phone-reachable adapter such as Wi-Fi or Ethernet instead of a
107virtual adapter. It also shows whether the Metro host is Expo-managed or came
108from an explicit `REACT_NATIVE_PACKAGER_HOSTNAME` override. It reports
109`serverOpenAIKeyLoaded: true` or `false`, but never prints the key value.
110​
111This example is native-only. Do not open `http://localhost:8081` expecting a
112web UI; port 8081 is Metro's manifest and bundle server, so seeing JSON there is
113expected.
114​
115Launch the app from the Expo terminal UI instead:
116​
117- Expo Go: scan the QR code with the Expo Go app on your device.
118- Android emulator: run `expo start --android` from this example or press `a` in
119 the Expo terminal UI.
120- iOS simulator on macOS: run `expo start --ios` from this example or press `i`
121 in the Expo terminal UI.
122​
123## Device URLs And Overrides
124​
125The app reads `EXPO_PUBLIC_TANSTACK_AI_BASE_URL` and defaults to `http://127.0.0.1:8787`.
126​
127`pnpm dev` sets this automatically for physical devices. Set it manually if the
128chosen IP is wrong, your computer has multiple network adapters, or you are
129running only `pnpm dev:app`:
130​
131- iOS simulator: `http://127.0.0.1:8787` usually works.
132- Android emulator: use `http://10.0.2.2:8787`.
133- Physical device: use your computer's LAN IP, for example `http://192.168.1.10:8787`.
134​
135Expo normally chooses the Metro host automatically. Only set
136`REACT_NATIVE_PACKAGER_HOSTNAME` to a LAN IP without a protocol or port for
137unusual network cases, such as Expo choosing a VPN or virtual adapter.
138​
139PowerShell example:
140​
141```powershell
142$env:EXPO_PUBLIC_TANSTACK_AI_BASE_URL = 'http://10.0.2.2:8787'
143pnpm --filter ts-react-native-chat dev
144```
145​
146Physical device PowerShell override example:
147​
148```powershell
149$env:REACT_NATIVE_PACKAGER_HOSTNAME = '192.168.1.10'
150$env:EXPO_PUBLIC_TANSTACK_AI_BASE_URL = 'http://192.168.1.10:8787'
151pnpm --filter ts-react-native-chat dev
152```
153​
154Custom server port example:
155​
156```powershell
157$env:PORT = '9999'
158pnpm --filter ts-react-native-chat dev
159```
160​
161cmd example:
162​
163```bat
164set EXPO_PUBLIC_TANSTACK_AI_BASE_URL=http://10.0.2.2:8787
165pnpm --filter ts-react-native-chat dev
166```
167​
168macOS/Linux example:
169​
170```sh
171EXPO_PUBLIC_TANSTACK_AI_BASE_URL=http://10.0.2.2:8787 pnpm --filter ts-react-native-chat dev
172```
173​
174## Troubleshooting Physical Devices
175​
176If Expo Go scans the QR code but stays stuck before the app starts:
177​
178- Run `pnpm --filter ts-react-native-chat dev -- --print-config` and confirm `metroHost` is `Expo-managed` unless you intentionally set `REACT_NATIVE_PACKAGER_HOSTNAME`. Also confirm `lanInterfaceName` is a physical adapter, not `127.0.0.1`, WSL, Docker, Hyper-V, VPN, or another virtual adapter.
179- From the phone browser, open `http://<lan-ip>:8787/health` and confirm it returns `{"ok":true}`.
180- Confirm the phone and computer are on the same Wi-Fi network and client isolation is disabled.
181- Allow Node.js through the firewall on private networks for Metro port `8081` and the Hono server port `8787`.
182- If your computer has multiple adapters or VPNs and Expo chooses the wrong Metro host, set `REACT_NATIVE_PACKAGER_HOSTNAME=<lan-ip>` manually. Keep or set `EXPO_PUBLIC_TANSTACK_AI_BASE_URL=http://<lan-ip>:8787` for the Hono backend.
183- If the app opens to a white screen, collect Metro terminal output and device runtime logs. The bundle URL is already controlled by Expo unless you set `REACT_NATIVE_PACKAGER_HOSTNAME`, so the next signal should be the JavaScript/native error shown in those logs.
184​
185## Troubleshooting Android Emulator Launches
186​
187If Expo cannot launch the Android emulator and prints Android SDK or `adb`
188messages, this is an environment issue rather than a TanStack AI transport
189failure. Confirm Android Studio installed the Android SDK, that an emulator is
190created in Device Manager, and that `adb` is available on `PATH`.
191​
192Common Windows paths to check:
193​
194- `%LOCALAPPDATA%\Android\Sdk\platform-tools\adb.exe`
195- `%ANDROID_HOME%\platform-tools\adb.exe`
196- `%ANDROID_SDK_ROOT%\platform-tools\adb.exe`
197​
198After changing SDK environment variables or `PATH`, restart the terminal and
199run:
200​
201```powershell
202adb devices
203pnpm --filter ts-react-native-chat dev
204```
205​
206If `adb devices` does not list an emulator or attached device, fix that before
207debugging app-level networking or streaming behavior.
208​
209## Validation
210​
211```bash
212pnpm --filter ts-react-native-chat typecheck
213pnpm --filter ts-react-native-chat smoke:expo
214pnpm --filter ts-react-native-chat verify:react-resolution
215pnpm --filter ts-react-native-chat test:dev-script
216```