note
This content is from the README file of https://github.com/flux0-ai/flux0-react-vite-minimal-demo.
flux0-react-vite-minimal-demo
A minimal demo showing how to use @flux0-ai/react in a Vite + React + TypeScript app with real-time streaming.
This example:
- Treats the session id as a route (
/sessions/{id}) - Does not use a router – uses
history.pushStateandpopstate - On Enter at
/, it creates a session, starts streaming, and pushes/sessions/{id}to the URL - If the URL is already
/sessions/{id}, it loads prior events and continues streaming the same session - Refreshing the page will load prior session events from the server, so the page survives refreshes
- Streams live updates from the server via
@flux0-ai/react - Knows how to render both messages and tool calls
- Includes a “New Session” button that resets state and goes back to
/
Quick Start
-
Set your Agent ID via environment variable In the root of your project, modify
.envfile and set theVITE_AGENT_IDvariable to the ID of an existing agent in your running Flux0 server:VITE_AGENT_ID=<your-agent-id>The app will read this value at runtime, so you don’t need to change any code in
App.tsx. -
Install dependencies
npm install -
Run the Flux0 server
Ensure your
flux0-serveris running on port8080(the default). If you run it on another port, updatevite.config.tsaccordingly. -
Start the Vite dev server
npm run dev- Type a message on
/and press Enter → a session is created, streaming starts, and the URL becomes/sessions/{id} - Click New Session → the app resets events and navigates to
/ - If you visit
/sessions/{id}directly or refresh, the app loads prior events and continues streaming that session (refreshes survive)
- Type a message on
How It Works
- The app uses the
useMessageStreamhook from@flux0-ai/react:const {
messages,
streaming,
error,
processing,
startStreaming,
stopStreaming,
emittedEvents,
resetEvents,
} = useMessageStream({ events: loadedEvents }); - Initial events are fetched from
GET /api/sessions/:id/eventswhen a session is present in the URL. - If the user hits Enter on
/, we callPOST /api/sessionswith:and expect a response{ "agent_id": "<value from VITE_AGENT_ID>", "title": "new session" }{ id: string }. We then push/sessions/{id}and callstartStreaming(id, text). - The New Session button:
history.pushState({}, "", "/")setSessionId(null)resetEvents()