Key Features • Motivation • How To Use • Credits • License
- 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.
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.
-
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)
-
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:
POST /polls
-
Request Body:
{ "title": "Qual melhor framework web para NodeJS?", "options": ["Express", "Fastify"] }
-
Response Body:
{ "pollId": "194cef63-2ccf-46a3-aad1-aa94b2bc89b0" }
POST /polls/{pollId}/votes
-
Request Body:
{ "pollOptionId": "c5271ca0-3c5b-4a81-af94-284bfbfd49b1" }
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 } ] } }
-
-
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 }
This project was made for NLW #14 Expert by Rocketseat and uses the following open source packages:
The MIT License (MIT) 2024 - Evandro Costa. Please have a look at the LICENSE for more details.
GitHub @evans-costa · LinkedIn @evandro-souzac