# aryankv08-png/medigrid- architecture

> Architecture diagram of the GitHub repository aryankv08-png/medigrid-, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aryankv08-png/medigrid-
- Repository: https://github.com/aryankv08-png/medigrid-

## Overview

MediGrid is a prototype for coordinating ambulances, hospital beds, and inter-hospital transfers. Ambulance, hospital, and control-room clients connect over WebSocket, register, and send location, bed, or transfer messages; the server applies rate limits, finds and reserves a suitable bed, and returns or broadcasts updates. HTTP also exposes health and emergency-request data. MySQL holds operational records, Redis supports limits, locks, and cross-instance fanout, and RabbitMQ plus a worker handle reliable events and transfer workflows. The README describes these capabilities; some server and worker implementation details were not sampled.

Main entry points: [`src/index.ts`](https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts) (HTTP and WebSocket server), [`src/worker.ts`](https://github.com/aryankv08-png/medigrid-/blob/main/src/worker.ts) (Event and transfer worker), [`src/rabbitmq.ts`](https://github.com/aryankv08-png/medigrid-/blob/main/src/rabbitmq.ts) (Reliable event publishing), [`src/db.ts`](https://github.com/aryankv08-png/medigrid-/blob/main/src/db.ts) (Database access and schema)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client interface"]
  node_server["HTTP and WebSocket server<br/>[index.ts]"]
  node_schemas["Message schemas<br/>[protocol.ts]"]
  node_requestfeed["Emergency request feed<br/>[index.ts]"]
end

subgraph group_coord["Coordination"]
  node_bedops["Bed reservation and dispatch<br/>[index.ts]"]
  node_transferapi["Transfer actions<br/>[index.ts]"]
  node_location["Location and hospital matching<br/>[index.ts]"]
  node_routing["Road distance calculation<br/>[routing.ts]"]
end

subgraph group_async["Async events"]
  node_eventpub["Reliable event publishing<br/>[rabbitmq.ts]"]
  node_broker["RabbitMQ"]
  node_eventqueues["Bed, transfer, dispatch queues<br/>[rabbitmq.ts]"]
  node_worker["Event and transfer worker<br/>[worker.ts]"]
end

subgraph group_infra["Infrastructure"]
  node_dbaccess["Database access and schema<br/>[db.ts]"]
  node_mysql[("MySQL")]
  node_redislib["Rate limits, locks, fanout<br/>[redis.ts]"]
  node_redis[("Redis")]
  node_osrm["OSRM routing service"]
end

node_clients(("Ambulance / hospital / control"))

node_clients -->|"connects / requests"| node_server
node_server -->|"validates messages"| node_schemas
node_server -->|"handles bed requests"| node_bedops
node_server -->|"handles transfer actions"| node_transferapi
node_server -->|"handles location updates"| node_location
node_server -->|"serves request feed"| node_requestfeed
node_server -->|"queries records"| node_dbaccess
node_server -->|"checks limits and locks"| node_redislib
node_server -->|"publishes events"| node_eventpub
node_server -->|"calculates route distance"| node_routing
node_bedops -->|"reserves beds"| node_dbaccess
node_transferapi -->|"publishes transfer actions"| node_eventpub
node_location -->|"estimates distance"| node_routing
node_dbaccess -->|"reads and writes"| node_mysql
node_redislib -->|"stores limits and locks"| node_redis
node_eventpub -->|"publishes messages"| node_broker
node_eventpub -->|"routes topic events"| node_eventqueues
node_worker -->|"consumes events"| node_broker
node_worker -->|"consumes bed and transfer events"| node_eventqueues
node_worker -->|"updates records"| node_dbaccess
node_worker -->|"locks and broadcasts"| node_redislib
node_routing -->|"requests road routes"| node_osrm

click node_server "https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts"
click node_schemas "https://github.com/aryankv08-png/medigrid-/blob/main/src/protocol.ts"
click node_bedops "https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts"
click node_transferapi "https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts"
click node_location "https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts"
click node_requestfeed "https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts"
click node_routing "https://github.com/aryankv08-png/medigrid-/blob/main/src/routing.ts"
click node_dbaccess "https://github.com/aryankv08-png/medigrid-/blob/main/src/db.ts"
click node_redislib "https://github.com/aryankv08-png/medigrid-/blob/main/src/redis.ts"
click node_eventpub "https://github.com/aryankv08-png/medigrid-/blob/main/src/rabbitmq.ts"
click node_eventqueues "https://github.com/aryankv08-png/medigrid-/blob/main/src/rabbitmq.ts"
click node_worker "https://github.com/aryankv08-png/medigrid-/blob/main/src/worker.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server,node_schemas,node_requestfeed toneBlue
class node_bedops,node_transferapi,node_location,node_routing toneAmber
class node_eventpub,node_broker,node_eventqueues,node_worker toneMint
class node_dbaccess,node_mysql,node_redislib,node_redis,node_osrm toneRose
class node_clients toneIndigo
```

## Components

### Client interface

- [HTTP and WebSocket server](https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts): `src/index.ts`
- [Message schemas](https://github.com/aryankv08-png/medigrid-/blob/main/src/protocol.ts): `src/protocol.ts`
- [Emergency request feed](https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts): `src/index.ts`

### Coordination

- [Bed reservation and dispatch](https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts): `src/index.ts`
- [Transfer actions](https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts): `src/index.ts`
- [Location and hospital matching](https://github.com/aryankv08-png/medigrid-/blob/main/src/index.ts): `src/index.ts`
- [Road distance calculation](https://github.com/aryankv08-png/medigrid-/blob/main/src/routing.ts): `src/routing.ts`

### Async events

- [Reliable event publishing](https://github.com/aryankv08-png/medigrid-/blob/main/src/rabbitmq.ts): `src/rabbitmq.ts`
- **RabbitMQ**
- [Bed, transfer, dispatch queues](https://github.com/aryankv08-png/medigrid-/blob/main/src/rabbitmq.ts): `src/rabbitmq.ts`
- [Event and transfer worker](https://github.com/aryankv08-png/medigrid-/blob/main/src/worker.ts): `src/worker.ts`

### Infrastructure

- [Database access and schema](https://github.com/aryankv08-png/medigrid-/blob/main/src/db.ts): `src/db.ts`
- **MySQL**
- [Rate limits, locks, fanout](https://github.com/aryankv08-png/medigrid-/blob/main/src/redis.ts): `src/redis.ts`
- **Redis**
- **OSRM routing service**

### Other components

- **Ambulance / hospital / control**

## Connections

- Ambulance / hospital / control → HTTP and WebSocket server: connects / requests (evidence: `README.md`)
- HTTP and WebSocket server → Message schemas: validates messages (evidence: `src/index.ts`)
- HTTP and WebSocket server → Bed reservation and dispatch: handles bed requests (evidence: `src/index.ts`)
- HTTP and WebSocket server → Transfer actions: handles transfer actions (evidence: `src/index.ts`)
- HTTP and WebSocket server → Location and hospital matching: handles location updates (evidence: `src/index.ts`)
- HTTP and WebSocket server → Emergency request feed: serves request feed (evidence: `src/index.ts`)
- HTTP and WebSocket server → Database access and schema: queries records (evidence: `src/index.ts`)
- HTTP and WebSocket server → Rate limits, locks, fanout: checks limits and locks (evidence: `src/index.ts`)
- HTTP and WebSocket server → Reliable event publishing: publishes events (evidence: `src/index.ts`)
- HTTP and WebSocket server → Road distance calculation: calculates route distance (evidence: `src/index.ts`)
- Bed reservation and dispatch → Database access and schema: reserves beds (evidence: `src/index.ts`)
- Transfer actions → Reliable event publishing: publishes transfer actions (evidence: `src/index.ts`)
- Location and hospital matching → Road distance calculation: estimates distance (evidence: `src/index.ts`)
- Database access and schema → MySQL: reads and writes (evidence: `src/db.ts`)
- Rate limits, locks, fanout → Redis: stores limits and locks (evidence: `src/redis.ts`)
- Reliable event publishing → RabbitMQ: publishes messages (evidence: `src/rabbitmq.ts`)
- Reliable event publishing → Bed, transfer, dispatch queues: routes topic events (evidence: `src/rabbitmq.ts`)
- Event and transfer worker → RabbitMQ: consumes events (evidence: `src/worker.ts`)
- Event and transfer worker → Bed, transfer, dispatch queues: consumes bed and transfer events (evidence: `src/worker.ts`)
- Event and transfer worker → Database access and schema: updates records (evidence: `src/worker.ts`)
- Event and transfer worker → Rate limits, locks, fanout: locks and broadcasts (evidence: `src/worker.ts`)
- Road distance calculation → OSRM routing service: requests road routes (evidence: `src/routing.ts`)
