Giter Club home page Giter Club logo

fastify-socket-server's Introduction

Fastify Socket Server

Table of Contents

Overview

This project is a Fastify application that implements CRUD operations for a User entity and integrates a WebSocket server to enable bidirectional communication between the client and server. The application includes Swagger documentation for the Fastify endpoints and a Postman collection for testing the WebSocket server.

Project Structure

  • ├── src
  • │ ├── controllers
  • │ │ └── userController.js
  • │ ├── db
  • │ │ └── db.js
  • │ ├── routes
  • │ │ └── user.route.js
  • │ ├── controllers
  • │ │ └── user.controller.js
  • │ ├── services
  • │ │ └── user.service.js
  • │ └── app.js
  • │── socket-server
  • │ └─── socketServer.js
  • │── prisma
  • │ ├── migrations
  • │ └─── schema.prisma
  • ├── .gitignore
  • ├── package.json
  • ├── package-lock.json
  • ├── docker-compose.yml
  • └── README.md `

Installation

  1. Clone the repository:

    $ git clone https://github.com/MohamedAEmara/fastify-socket-server.git

    $ cd fastify-socket-server

  2. Install the dependencies:

    npm install

  3. Add .env file and add your enviroment variables

    HOST=localhost
    PORT=3000
    PASSWORD_SALT="$fdb$10$wH8qdfswE5R3C.6v5fdsZ/ZmM"
    JWT_SECRET="secretvalue123"
    DATABASE_URL="postgresql://postgres:1234@localhost:5432/fastifysocket"
    
    ## NOTE: this data base will connect to DB docker container defined in docker-compse.

Make sure you installed Postgres on your machine

OR

Use the DB configurations in docker-compose.yml file (Make sure you installed Docker)

Running the Application

  1. Start the Fastify server: this will run DB container and connect fastify server to it. Also will run the socker server

    npm run start:dev

    In case you don't want to use Docker and have Postgres installed on your machine RUN

    npm run start

  2. The server will be running at http://localhost:3000.

API Documentation

You can see Swagger documentation and try it out through the deployed server

OR

You can run the server locally and see Swagger documentation at /documentaion endpoint

User Endpoints

  • GET /users: Retrieve all users
  • GET /users/:id: Retrieve a user by ID
  • POST /users: Create a new user
  • PUT /users/:id: Update a user by ID
  • DELETE /users/:id: Delete a user by ID

WebSocket Communication

The WebSocket server is implemented using the ws library. It enables bidirectional communication between the client and server.

How to Test WebSocket

  1. Open Postman.
  2. Create a new WebSocket request to ws://localhost:3000.
  3. Send a message to the server.
  4. The server will log the received message and send a response back to the client.

For more detailed testing, you can use the provided Postman collection.

Testing

To test the WebSocket server using Postman:

  1. Import the Postman collection from test/socketTest.json.
  2. Establish a WebSocket connection to ws://localhost:3000.
  3. Send test messages and verify the responses.

Postman Testing

33e4-d94c-47f3-963e-b66a52d54de1.mp4

Dependencies

  • Fastify - Fast and low overhead web framework for Node.js
  • ws - Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
  • fastify-swagger - Fastify plugin to serve Swagger UI and JSON
  • bcrypt - For hashing user passwords
  • @prisma/client - ORM For easier dealing with postgres

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.

Contact

For any inquiries or support, please contact Mohamed Emara.

fastify-socket-server's People

Contributors

mohamedaemara avatar

Watchers

 avatar

fastify-socket-server's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.