A lightweight, real-time, two-way chat widget built with a Go (Golang) backend and a Svelte 5 frontend.
This project intentionally avoids heavy, opinionated frameworks (like the Vercel AI SDK) in favor of standard, native WebSockets for true, bidirectional "push" communication between the server and the browser.
Architecture
- Backend: Go +
gorilla/websocket
- Holds a persistent, permanent connection open with the client.
- Capable of instantly "pushing" messages to the frontend without the user initiating a request (e.g., proactive greetings, AI streaming, or human-agent handoffs).
- Runs on port
8081.
- Frontend: Svelte 5 (Vite + TailwindCSS)
- A modern, reactive UI component (
chat.svelte).
- Manages its own chat history state natively.
- Uses native browser
WebSocket APIs to send and receive JSON payloads.
Getting Started
You will need to run both the backend and frontend servers simultaneously during development.
1. Start the Go Backend
Navigate to the backend directory, install dependencies, and run the server:
cd backend
go mod tidy
go run main.go
The WebSocket server will start listening on ws://localhost:8081/ws.
2. Start the Svelte Frontend
In a separate terminal, navigate to the frontend directory, install dependencies, and start the Vite development server:
cd frontend
npm install
npm run dev
The application will be available at http://localhost:5173.
How it Works
- Connection: When the Svelte widget mounts, it immediately opens a WebSocket connection to the Go backend.
- Server Push: To demonstrate bidirectional capabilities, the Go backend will instantly push a proactive "Welcome" message down the pipe the moment a user connects.
- Echoing: When a user types a message in the widget, it is sent as a JSON string to the backend. The Go server reads it and echoes it back with a
Go Echo: prefix.
Future Enhancements
- Integrate an actual LLM (like OpenAI or Anthropic) into the Go backend's
Listen Loop instead of the simple echo.
- Implement chat history persistence (e.g., saving sessions to PostgreSQL or Redis) on the backend.
- Add connection heartbeat/ping logic to gracefully handle unstable network conditions.