Skip to content

A poll app with real-time communication via websockets

License

Notifications You must be signed in to change notification settings

evans-costa/polls-realtime

Repository files navigation


Poll Creator

A poll creator using Fastify, Redis and real time voting results with WebSockets

Typescript NodeJS Redis Fastify Postgres Prisma

Key FeaturesMotivationHow To UseCreditsLicense

Key Features

  • Create a poll with title and the options to vote using PostgreSQL with Prisma as a ORM.
  • Get the poll created with number of votes ranked.
  • Voting on a poll, the number of each votes will be stored using Redis as a database.
  • When accessing the WebSocket endpoint, you will subscribe to the results of this poll.
  • Everytime you vote, if a WebSocket connection is estabilished, all subscribers get the results in real time.

Motivation

How real-time communication works is important, many modern applications and services works with WebSockets, e.g. real-time chat. I learned the Pub/Sub pattern, when a Subscriber makes an inscription on a channel or topic and a Publisher publishes the message in this channel and the Subscribers consumes this message. For that application, using Redis as an in-memory database was very important to learn the use-cases of this database and, because of its fast read and write operations, it is one of the first choices on a chat application and message systems.

How To Use

  • To clone and run this application, you'll need Git, Node.js (which comes with npm) and Docker with Docker Compose installed on your computer. From your command line:

    # Clone this repository
    $ git clone https://github.com/evans-costa/polls-realtime.git
    
    # Go into the repository
    $ cd polls-realtime
    # Install dependencies
    $ npm install
  • Rename the .env.example file to .env and fill it accordingly:

    PORT=<YOUR_PORT>
    
    POSTGRES_USER=local_user
    POSTGRES_PASSWORD=local_password
    POSTGRES_DB=polls
    POSTGRES_PORT_NUMBER=54325
    
    DATABASE_URL=postgresql://local_user:local_password@localhost:54325/polls?schema=public
    
    SECRET_KEY=<YOUR_SECRET_KEY>
  • Run the command to up Postgres and Redis containers:

    # Up the containers
    $ npm run services:up

    This will up a Redis and a PostgreSQL container on your Docker installation.

  • Run the Prisma migrations:

    # Run the migrations
    $ npm run migrate:dev
  • Run the server:

    # Run the project
    $ npm run dev
  • If you would like to access the database by a GUI, Prisma comes with a built-in GUI to view and edit the data, from your command line:

    # Access the Prisma Studio
    $ npm run prisma:studio
  • Now you can test it in your favorite API Testing Platform (Insomnia, Postman, Hoppscotch)

HTTP endpoints

  • The API will be accessible at http://localhost:3000 (or in the port setted in .env file)

  • The API provides the following HTTP endpoints and the body requests / responses:

    Creating a poll:

    POST /polls
    • Request Body:

      {
        "title": "Qual melhor framework web para NodeJS?",
        "options": ["Express", "Fastify"]
      }
    • Response Body:

      {
        "pollId": "194cef63-2ccf-46a3-aad1-aa94b2bc89b0"
      }

    Creating a vote on a poll:

    POST /polls/{pollId}/votes
    • Request Body:

      {
        "pollOptionId": "c5271ca0-3c5b-4a81-af94-284bfbfd49b1"
      }

    Get a poll by its ID:

    GET /polls/{pollId}
    • Response Body:

      {
        "poll": {
          "id": "194cef63-2ccf-46a3-aad1-aa94b2bc89b0",
          "title": "Qual a melhor framework web para NodeJS?",
          "options": [
            {
              "id": "c5271ca0-3c5b-4a81-af94-284bfbfd49b1",
              "title": "Express",
              "score": 2
            },
            {
              "id": "780b8e25-a40e-4301-ab32-77ebf8c79da8",
              "title": "Fastify",
              "score": 1
            }
          ]
        }
      }

WebSocket endpoint

  • The WebSocket endpoint is at:

    ws://localhost:3000/polls/{pollId}/votes
  • Everytime when voted on a poll, the WebSocket endpoint will publish the following message:

    {
      "pollOptionId": "c5271ca0-3c5b-4a81-af94-284bfbfd49b1",
      "votes": 2
    }

Credits

This project was made for NLW #14 Expert by Rocketseat and uses the following open source packages:

License

The MIT License (MIT) 2024 - Evandro Costa. Please have a look at the LICENSE for more details.


GitHub @evans-costa  ·  LinkedIn @evandro-souzac

About

A poll app with real-time communication via websockets

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published