A scalable real-time polling system built with Node.js, Express, PostgreSQL, Kafka, and WebSocket. This system allows users to create polls, cast votes, and get real-time updates on voting results.
- Create and manage polls with multiple options
- Real-time vote tracking using WebSocket
- Event-driven architecture using Kafka
- RESTful API endpoints
- Leaderboard system with time-based filtering
- Comprehensive statistics and analytics
- Database persistence with PostgreSQL
- Scalable and maintainable codebase
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
- Apache Kafka
- npm or yarn
git clone [repository-url]
cd polling-system
npm install
Create a .env
file in the root directory and add the following configurations:
PORT=3000
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=polling_system
CREATE DATABASE polling_system;
To start the Zookeeper server, run the following command:
bin/zookeeper-server-start.sh config/zookeeper.properties
To start the Kafka, run the following command:
bin/kafka-server-start.sh config/server.properties
bin/kafka-topics.sh --create --topic poll-votes --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
src/
├── config/
│ ├── database.js
│ └── kafka.js
├── controllers/
│ ├── pollController.js
│ ├── voteController.js
│ └── leaderboardController.js
├── models/
│ ├── index.js
│ ├── Poll.js
│ ├── Option.js
│ └── Vote.js
├── routes/
│ ├── pollRoutes.js
│ ├── voteRoutes.js
│ └── leaderboardRoutes.js
├── services/
│ ├── pollService.js
│ ├── voteService.js
│ ├── kafkaService.js
│ ├── websocketService.js
│ └── leaderboardService.js
└── server.js
POST /api/v1/polls/createPoll
- Create a new poll
GET /api/v1/polls/getPoll/:id
- Get poll by ID
GET /api/v1/polls/allpolls
- List all polls
- Cast a vote for an option
GET /api/v1/leaderboard/:pollId?
- Get a leaderboard for a specific poll
GET /api/v1/leaderboard/:pollId/option/:optionId
- Get the leaderboard for a specific option within a poll
// Cast a vote
{
type: 'vote',
data: {
userId: 'string',
optionId: 'string'
}
}
// Vote update
{
type: 'vote_update',
data: {
optionId: 'string',
newVoteCount: number
}
}
npm start
node src/testWebSocketClient.js
To run the test WebSocket client:
node src/testWebSocketClient.js
-
Input validation
-
Database connection errors
-
WebSocket connection errors
-
Kafka producer/consumer errors
-
Duplicate vote prevention
-
Fork the repository
-
Create your feature branch (git checkout -b feature/AmazingFeature)
-
Commit your changes (git commit -m 'Add some AmazingFeature')
-
Push to the branch (git push origin feature/AmazingFeature)
-
Open a Pull Request
This project is licensed under the MIT License.