Open History to see the last commit
1# Vanilla Chat Example
2
3A simple vanilla JavaScript chat interface using `@tanstack/ai-client`.
4
5## Features
6
7- ✅ Pure vanilla JavaScript (no frameworks)
8- ✅ Uses `@tanstack/ai-client` for chat functionality
9- ✅ Real-time streaming messages
10- ✅ Beautiful, responsive UI :)
11
12## Setup
13
141. **Install dependencies:**
15
16```bash
17cd examples/vanilla-chat
18npm install
19# or
20pnpm install
21```
22
232. **Point the client at your chat backend:**
24
25Configure your own backend that streams Server-Sent Events to the URL referenced in `src/main.js`.
26
273. **Start the Vite dev server:**
28
29```bash
30npm run dev
31# or
32pnpm dev
33```
34
35The app will be available at `http://localhost:3001`
36
37## Usage
38
391. Open `http://localhost:3001` in your browser
402. Type a message and press Enter (or click Send)
413. Watch the AI response stream in real-time!
42
43## Project Structure
44
45```
46vanilla-chat/
47├── index.html # Main HTML file
48├── src/
49│ ├── main.js # Chat client logic
50│ └── style.css # Styles
51├── package.json # Dependencies
52└── vite.config.ts # Vite configuration
53```
54
55## How It Works
56
57The app uses `ChatClient` from `@tanstack/ai-client` with the `fetchServerSentEvents` connection adapter to connect to a chat backend:
58
59```javascript
60import { ChatClient, fetchServerSentEvents } from '@tanstack/ai-client'
61
62const client = new ChatClient({
63 connection: fetchServerSentEvents('http://localhost:8080/chat'),
64 onMessagesChange: (messages) => {
65 // Update UI when messages change
66 },
67 onLoadingChange: (isLoading) => {
68 // Update loading state
69 },
70})
71```
72
73The backend streams responses in Server-Sent Events (SSE) format, which the client automatically parses and displays.