A real-time streaming chat application powered by Google Gemini API and Cloudflare Workers.
- Real-time text streaming from Gemini API
- Clean, responsive web interface
- Serverless backend using Cloudflare Workers
- TypeScript support
- Node.js (v18 or higher)
- pnpm (or npm)
- Cloudflare account
- Google AI API key
git clone <your-repo-url>
cd stream-chatpnpm installCreate a .env file in the worker directory:
# apps/worker/.env
GEMINI_API_KEY=your_google_ai_api_key_here- Start the Cloudflare Worker locally:
cd apps/worker
npx wrangler devThis will start the worker locally and provide a URL like http://localhost:8787
- Update the worker URL in the web app:
In apps/web/src/App.tsx, update the WORKER_URL constant:
const WORKER_URL = 'http://localhost:8787/chat';- Start the web application:
cd apps/web
pnpm devThe application will be available at http://localhost:5173
- Deploy the Cloudflare Worker:
cd apps/worker
npx wrangler deploy- Update the worker URL:
In apps/web/src/App.tsx, update the WORKER_URL constant with your deployed worker URL:
const WORKER_URL = 'https://your-worker.your-subdomain.workers.dev/chat';- Build and serve the web application:
cd apps/web
pnpm build
pnpm preview- Open the web application in your browser
- Enter a prompt in the text area
- Click "Send Prompt" to start streaming
- Watch the response appear in real-time as Gemini generates it
stream-chat/
├── apps/
│ ├── web/ # React frontend
│ └── worker/ # Cloudflare Worker backend
├── package.json # Root package.json
└── README.md
The worker exposes a single endpoint:
POST /chat- Streams responses from Gemini API
Request body:
{
"contents": [
{
"parts": [
{
"text": "Your prompt here"
}
]
}
]
}- Terminal 1 - Start the worker:
cd apps/worker
npx wrangler dev- Terminal 2 - Start the web app:
cd apps/web
pnpm dev- Update the worker URL in the web app (if needed):
- Open
apps/web/src/App.tsx - Change
WORKER_URLto'http://localhost:8787/chat'
- Open
For local development, create a .env file in the worker directory:
# apps/worker/.env
GEMINI_API_KEY=your_google_ai_api_key_here# Build web app
cd apps/web
pnpm build
# Deploy worker
cd apps/worker
npx wrangler deployMIT