Skip to content

RamaAditya49/LivePollHub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Real-Time Polling Platform

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!

Features

  • 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.

Tech Stack

  • Backend: Node.js, Express.js, PostgreSQL, WebSockets (ws library)
  • Frontend: HTML, CSS, Vanilla JavaScript
  • Testing: Mocha, Chai, Supertest (for backend)

Project Structure

.
├── 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

Prerequisites

  • Node.js (v14.x or later recommended)
  • npm (usually comes with Node.js)
  • PostgreSQL (running instance)

Setup and Installation

  1. Clone the repository:

    git clone https://github.com/RamaAditya49/LivePollHub
    cd <repository-directory>
  2. 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 like psql:
        CREATE DATABASE polling_app;
      • Create a .env file in the backend directory by copying .env_example:
        cp .env_example .env
      • Edit backend/.env with your PostgreSQL connection details (username, password, host, port) and a secure JWT_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_app database when the backend server starts for the first time. Alternatively, you can manually run the SQL commands from database/schema.sql.
  3. 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_MAIN and WS_URL constants in frontend/js/main.js (and API_BASE_URL in frontend/js/auth.js) are pointing to your backend server's address (default is http://localhost:3000 and ws://localhost:3000).

Running the Application

  1. Start the Backend Server:

    • Navigate to the backend directory:
      cd backend
    • Start the server:
      npm start
      (This assumes you have a "start": "node index.js" script in backend/package.json. If not, use node index.js)
    • The backend server will typically run on http://localhost:3000.
  2. Run the Frontend:

    • Open the frontend/index.html file in your web browser.
    • You can serve the frontend directory using a simple HTTP server if you prefer, for example, using the http-server npm package:
      # If you don't have http-server, install it globally (or use npx)
      # npm install -g http-server
      cd frontend
      http-server -p 8080
      Then access the app at http://localhost:8080.

Running Tests (Backend)

  • 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 backend directory:
    cd backend
  • Run the tests:
    npm test

API Endpoints Overview

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_update messages when a poll's votes change.

Contributing

Contributions are highly welcome! Whether it's bug fixes, new features, or improvements to documentation, please feel free to:

  1. Fork the repository.
  2. Create a new branch for your feature or fix: git checkout -b feature/your-feature-name or bugfix/your-bug-fix.
  3. Make your changes. Ensure your code follows the existing style and add tests for new functionality if applicable.
  4. Commit your changes: git commit -m "Add some amazing feature".
  5. Push to the branch: git push origin feature/your-feature-name.
  6. Open a Pull Request against the main branch of this repository.

Please provide a clear description of your changes in the pull request.

License

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!)

About

A real-time polling platform. Users create polls, and visitors vote instantly, with results updating live.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors