A real-time todo application demonstrating Electric SQL integration with Next.js, featuring automatic data synchronization between the frontend and PostgreSQL database.
- Real-time Sync: Automatic data synchronization using Electric SQL
- Optimistic Updates: Instant UI updates with rollback on errors
- Modern UI: Clean interface built with Tailwind CSS and shadcn/ui
- Type Safety: Full TypeScript support with Zod validation
- Live Queries: Real-time data updates using TanStack Query
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Next.js App βββββΊβ Electric SQL βββββΊβ PostgreSQL β
β (Frontend) β β (Sync Layer) β β (Database) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
- Node.js 18+
- Docker and Docker Compose
- Git
git clone <repository-url>
cd fe-sync-electricdocker-compose up -dThis starts:
- PostgreSQL database on port 54321
- Electric SQL sync service on port 3000
cd front-end
npm installnpm run devThe application will be available at http://localhost:3001
- Open your browser and navigate to
http://localhost:3001 - You should see the "Electric SQL Todo Sync" interface
- The page should display "No todos yet. Add one above!"
- Type "Buy groceries" in the input field
- Click the "Add" button
- The todo should appear instantly in the list
- Check the browser console to see sync logs
- Open a second browser tab/window to the same URL
- Add another todo: "Walk the dog"
- Both tabs should show the new todo simultaneously
- This demonstrates real-time synchronization
- Connect to the PostgreSQL database:
docker exec -it electric_quickstart-postgres-1 psql -U postgres -d electric- Query the todos table:
SELECT * FROM todos ORDER BY id DESC;- You should see all todos you've created
- Stop the Electric service:
docker-compose stop electric - Try to add a new todo
- Notice the optimistic update and subsequent rollback
- Restart Electric:
docker-compose start electric
The application uses the following default configuration:
- PostgreSQL:
localhost:54321 - Database:
electric - User:
postgres - Password:
password - Electric Sync:
http://localhost:3000/v1/shape
To modify the database connection, update the pool configuration in front-end/app/api/todos/route.ts:
const pool = new Pool({
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '54321'),
database: process.env.DB_NAME || 'electric',
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'password',
});fe-sync-electric/
βββ docker-compose.yml # Infrastructure services
βββ front-end/
β βββ app/
β β βββ api/
β β β βββ todos/
β β β βββ route.ts # REST API endpoints
β β βββ globals.css # Global styles
β β βββ layout.tsx # Root layout
β β βββ page.tsx # Main todo application
β βββ components/
β β βββ ui/ # shadcn/ui components
β βββ lib/
β β βββ utils.ts # Utility functions
β βββ package.json # Dependencies and scripts
βββ README.md # This file
- Frontend: Next.js 15, React 19, TypeScript
- Styling: Tailwind CSS, shadcn/ui
- Database: PostgreSQL 16
- Sync: Electric SQL, TanStack Query
- Validation: Zod
- Development: Biome (linting/formatting)
- User Input: User types a todo and clicks "Add"
- Optimistic Update: Todo appears immediately in the UI
- API Call: Request sent to Next.js API route
- Database Insert: Todo saved to PostgreSQL
- Electric Sync: Electric SQL detects the change
- Live Query Update: TanStack Query receives the update
- UI Update: All connected clients see the change
- Electric SQL monitors PostgreSQL for changes
- TanStack Query provides live queries that automatically update
- Optimistic updates provide instant feedback
- Error handling includes rollback functionality
- Set up a production PostgreSQL database
- Configure Electric SQL for production
- Update environment variables
- Build and deploy the Next.js application
- Use environment variables for sensitive data
- Enable SSL for database connections
- Configure proper CORS settings
- Implement authentication if needed
-
Database Connection Failed
- Ensure Docker containers are running
- Check port 54321 is available
- Verify database credentials
-
Electric Sync Not Working
- Check Electric service is running on port 3000
- Verify shape endpoint is accessible
- Check browser console for errors
-
Optimistic Updates Not Rolling Back
- Ensure error handling is working
- Check network connectivity
- Verify API endpoint is responding
# Check container status
docker-compose ps
# View Electric logs
docker-compose logs electric
# View PostgreSQL logs
docker-compose logs postgres
# Restart services
docker-compose restartThis project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For questions or issues:
- Check the Electric SQL documentation
- Review the TanStack Query docs
- Open an issue in this repository