A dynamic and interactive real-time polling application where users can create polls, and visitors can vote instantly with results updating live. This platform is designed to be engaging and easy to use, built with a modern tech stack.
The project is open-source, and contributions from the community are welcome to make it even more awesome!
- User Authentication: Secure registration and login for users.
- Poll Creation: Authenticated users can create new polls with multiple options.
- Instant Voting: Users can vote on polls, and their choices are recorded immediately.
- Real-Time Results: Poll results update live for all connected clients as votes are cast, powered by WebSockets.
- View Polls: Users can see a list of all available polls and view details for specific polls.
- API Backend: A robust backend API built with Node.js and Express.js.
- Simple Frontend: A clean frontend built with HTML, CSS, and vanilla JavaScript.
- Database: PostgreSQL is used for data persistence.
- Tested: Backend API includes a suite of tests using Mocha, Chai, and Supertest.
- Backend: Node.js, Express.js, PostgreSQL, WebSockets (
wslibrary) - Frontend: HTML, CSS, Vanilla JavaScript
- Testing: Mocha, Chai, Supertest (for backend)
.
├── backend/ # Node.js Express backend
│ ├── test/ # Backend tests
│ ├── node_modules/
│ ├── index.js # Main backend server file
│ ├── package.json
│ └── ...
├── database/ # Database schema
│ └── schema.sql
├── frontend/ # HTML, CSS, JS frontend
│ ├── css/
│ ├── js/
│ ├── index.html
│ └── ...
├── LICENSE # Project License (MIT)
└── README.md
- Node.js (v14.x or later recommended)
- npm (usually comes with Node.js)
- PostgreSQL (running instance)
-
Clone the repository:
git clone https://github.com/RamaAditya49/LivePollHub cd <repository-directory>
-
Backend Setup:
- Navigate to the backend directory:
cd backend - Install dependencies:
npm install
- Database Configuration:
- Ensure PostgreSQL is running.
- Create a database named
polling_app. You can use a tool likepsql:CREATE DATABASE polling_app;
- Create a
.envfile in thebackenddirectory by copying.env_example:cp .env_example .env
- Edit
backend/.envwith your PostgreSQL connection details (username, password, host, port) and a secureJWT_SECRET.DB_USER=your_db_user DB_HOST=localhost DB_NAME=polling_app DB_PASSWORD=your_db_password DB_PORT=5432 JWT_SECRET=a_very_strong_and_long_secret_key_please_change_me PORT=3000 # Optional, defaults to 3000
- The application will attempt to create the necessary tables in the
polling_appdatabase when the backend server starts for the first time. Alternatively, you can manually run the SQL commands fromdatabase/schema.sql.
- Navigate to the backend directory:
-
Frontend Setup:
- The frontend consists of static HTML, CSS, and JavaScript files. No special build process is required for the current setup.
- Ensure the
API_BASE_URL_MAINandWS_URLconstants infrontend/js/main.js(andAPI_BASE_URLinfrontend/js/auth.js) are pointing to your backend server's address (default ishttp://localhost:3000andws://localhost:3000).
-
Start the Backend Server:
- Navigate to the
backenddirectory:cd backend - Start the server:
(This assumes you have a "start": "node index.js" script in
npm start
backend/package.json. If not, usenode index.js) - The backend server will typically run on
http://localhost:3000.
- Navigate to the
-
Run the Frontend:
- Open the
frontend/index.htmlfile in your web browser. - You can serve the
frontenddirectory using a simple HTTP server if you prefer, for example, using thehttp-servernpm package:Then access the app at# If you don't have http-server, install it globally (or use npx) # npm install -g http-server cd frontend http-server -p 8080
http://localhost:8080.
- Open the
- Ensure the backend server is NOT running, or run tests on a different port/DB if your tests require a live server managed by supertest externally. The current tests make requests to
http://localhost:3000, so the main backend server should be running.- (Correction for typical test setups: Tests often start and stop their own server instance or use a dedicated test database. The current tests assume the main dev server is running. For more robust testing, the tests would manage the server lifecycle.)
- Navigate to the
backenddirectory:cd backend - Run the tests:
npm test
The backend provides the following API endpoints:
- Authentication
POST /auth/register: Register a new user.POST /auth/login: Login an existing user, returns a JWT.
- Polls
POST /api/polls: Create a new poll (Authentication required).GET /api/polls: Get a list of all polls.GET /api/polls/:pollId: Get details for a specific poll.POST /api/polls/:pollId/vote: Cast a vote on a poll (Authentication required).
- Protected Route Example
GET /api/protected: Example route to test JWT authentication.
WebSockets The server also uses WebSockets for real-time communication:
- Connect to
ws://localhost:3000. - Receives
poll_updatemessages when a poll's votes change.
Contributions are highly welcome! Whether it's bug fixes, new features, or improvements to documentation, please feel free to:
- Fork the repository.
- Create a new branch for your feature or fix:
git checkout -b feature/your-feature-nameorbugfix/your-bug-fix. - Make your changes. Ensure your code follows the existing style and add tests for new functionality if applicable.
- Commit your changes:
git commit -m "Add some amazing feature". - Push to the branch:
git push origin feature/your-feature-name. - Open a Pull Request against the
mainbranch of this repository.
Please provide a clear description of your changes in the pull request.
This project is licensed under the MIT License.
Buat yang keren untuk publik juga bisa mengembangkan! (Let's make something cool that the public can also develop!)