A real-time counter application with PostgreSQL persistence and Server-Sent Events (SSE) for live updates across multiple users.
- Real-time synchronization: When one user increments the counter, all connected users see the update instantly
- PostgreSQL persistence: Counter value is stored in a database and persists between server restarts
- Server-Sent Events (SSE): Lightweight, HTTP-based real-time communication
- Multi-user support: Multiple users can interact with the same counter simultaneously
- Simple web interface: Clean, responsive frontend with increment button
- Frontend: Users click the increment button which sends a POST request to
/increment - Backend: Server updates the counter in PostgreSQL and broadcasts the new value via SSE
- Real-time updates: All connected clients receive the update through their SSE connection (
/events) - Live synchronization: Counter updates appear instantly on all open browser tabs/windows
-
Install PostgreSQL:
brew install postgresql brew services start postgresql
-
Create the database:
createdb counter_app psql -d counter_app -f setup-db.sql
-
Install dependencies:
npm install
-
Start the server:
npm start
-
Open your browser: Navigate to
http://localhost:8081
To test the real-time functionality:
- Open multiple browser tabs/windows to
http://localhost:8081 - Click the increment button in any tab
- Watch the counter update instantly in all other tabs
- Try opening the app on different devices on the same network
- HTTP server serving static files and API endpoints
- PostgreSQL integration for data persistence
- SSE endpoint (
/events) for real-time communication - Broadcasts counter updates to all connected clients
- EventSource connection to
/eventsfor real-time updates - Button click sends POST to
/incrementendpoint - Automatic counter updates via SSE messages
- Simple counter table with single row
- Atomic increment operations for consistency
The application expects PostgreSQL to be running on:
- Host: localhost
- Port: 5432
- Database: counter_app
- User: etienne (or your system username)
You can modify these settings in server.js if needed.
server.js- Backend server with PostgreSQL and SSE integrationindex.html- Frontend web interface with EventSource connectionsetup-db.sql- Database initialization scriptpackage.json- Node.js dependencies.gitignore- Git ignore file for Node.js projects