2
Watch
3
Star
2
Fork
7
Issue

Hedwig, the messenger 🦉💬 - an asynchronous web-server to support chat-applications having multiple rooms/groups

shawarmaKoders
shawarmaKoders
pushedAt 5 months ago

shawarmaKoders/Hedwig

Hedwig 🦉💬

Hedwig, the messenger is an asynchronus web-server to support chat-applications having multiple rooms/groups, each having two or more participants.

Hedwig is built using FastAPI and websockets. MongoDB is used as the persistent storage, whereas Redis is used as the caching layer for messages - its Pub/Sub feature is used for triggering sending/receiving of messages.

Flow-Diagram

Use-Case

Ideal use-case of Hedwig would be when you have to add chat-rooms in various parts of your app or website, which is not chatting platform per se.
Hedwig opens a new webosocket connection for every chat room. This has been done intentionally so that resources are not wasted (in maintaining websocket-connections) when chatting features are not being used.

Usage

  • Install the dependencies (mentioned in pyproject.toml and poetry.lock)
    If you're using poetry, just run poetry install
  • The project needs a mongoDB server and a Redis server running.
    Hedwig picks up these environment variables: MONGODB_URI, REDIS_URL
    You can also just create a .env file and add them like these so you don't have to set them again and again
MONGODB_URI=...
REDIS_URL=...
  • Run the server
uvicorn main:app
  • Create a chat-room by making a POST request on /chat-room/create
  • Open a websocket connection and chat on /chat-room/{chat_room_id}/chat?user_id={user_id}
    • Incoming Messages Format:
      {
        "time": timestamp,
        "text": "text message"
      }
    • Outgoing Messages Format:
      {
        "user": "user_id",
        "time": timestamp,
        "text": "text message"
      }
ucloud ads