examples/vanilla-chat
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
Vanilla Chat Example#
A simple vanilla JavaScript chat interface using @tanstack/ai-client.
Features#
- ✅ Pure vanilla JavaScript (no frameworks)
- ✅ Uses
@tanstack/ai-clientfor chat functionality - ✅ Real-time streaming messages
- ✅ Beautiful, responsive UI :)
Setup#
- Install dependencies:
cd examples/vanilla-chat
npm install
# or
pnpm install
- Point the client at your chat backend:
Configure your own backend that streams Server-Sent Events to the URL referenced in src/main.js.
- Start the Vite dev server:
npm run dev
# or
pnpm dev
The app will be available at http://localhost:3001
Usage#
- Open
http://localhost:3001in your browser - Type a message and press Enter (or click Send)
- Watch the AI response stream in real-time!
Project Structure#
vanilla-chat/
├── index.html # Main HTML file
├── src/
│ ├── main.js # Chat client logic
│ └── style.css # Styles
├── package.json # Dependencies
└── vite.config.ts # Vite configuration
How It Works#
The app uses ChatClient from @tanstack/ai-client with the fetchServerSentEvents connection adapter to connect to a chat backend:
import { ChatClient, fetchServerSentEvents } from '@tanstack/ai-client'
const client = new ChatClient({
connection: fetchServerSentEvents('http://localhost:8080/chat'),
onMessagesChange: (messages) => {
// Update UI when messages change
},
onLoadingChange: (isLoading) => {
// Update loading state
},
})
The backend streams responses in Server-Sent Events (SSE) format, which the client automatically parses and displays.