Skip to content

A WebSocket-based chat application built with FastAPI

License

Notifications You must be signed in to change notification settings

houshmand-2005/chat_app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat App

A WebSocket-based chat application built with FastAPI

Table of Contents

Overview

This project is a WebSocket chat application created using FastAPI. It allows users to chat with others asynchronously in real-time, join groups, send and receive messages, and more.

FrontEnd Connect to websocket and api with raw JavaScript and for styling uses bootstrap and css

How It Works

By sending information with WebSockets, user can receive unread and new messages and send messages and also receive text changes such as edit and delete.

Also broadcast 'changes' and 'Send Message' are similar to this

you can see how change broadcast work (Codes have been shortened for display. See here for full codes: websocket.py)

async def broadcast_changes(
    group_id: int,
    message_id: int,
    new_text: str | None = None,
    change_type: models.ChangeType,
    db: Session,
) -> None:
  ...
  online_users = set(websocket_connections.keys())
  await asyncio.gather(
      *[
          send_change_to_user(
              member.user.id, changed_value, online_users=online_users
          )
          for member in group.members
      ]
  )

async def send_change_to_user(
    user_id: int, change_data: dict, online_users: set
) -> None:
  ...
  await connection.send_text(json.dumps(change_data))

Features

  • Real-time WebSocket chat
  • Written as async
  • User authentication and authorization
  • Group creation and management
  • Sending and receiving messages
  • Editing and deleting messages

How to run?

we need to get three docker images

  • 1.Python to run BackEnd and Uvicorn
  • 2.Nginx to run FrontEnd
  • 3.Postgres to run DataBase
# clone the project
git clone https://github.com/houshmand-2005/chat_app
# go to the folder
cd chat_app
# run project with docker
docker-compose up -d

Samples


This is list of available APIs:

ToDo

These are the things I want to do

  • Add validation (like email validation username validation and ...)
  • Make schemas with Pydantic for each model(now it is a little incomplete and some models don't have it)
  • Make FrontEnd more beautiful and make it more clear (This will probably be hard. I'm not very good at css and html😁)
  • Support sending photos and files
  • Support replay on message
  • Add a cache service for handel unread messages and more (Like redis)