How esp32async/espasyncwebserver works
Project type and purpose
• This is an embedded library (“ESPAsyncWebServer”) for providing an asynchronous HTTP and WebSocket server on microcontrollers (ESP32, ESP8266, RP2040/RP2350).
• It exposes non-blocking request handling, middleware, SSE (Server-Sent Events), WebSocket, JSON/MessagePack streaming, authentication, file upload/serving, etc.
File structure
• Top-level: configuration files (CMakeLists.txt, platformio.ini), CI workflows, examples, documentation.
• src/: core library modules (network glue, request/response, handlers, middleware, WebSocket, event source, JSON/MessagePack support, authentication).
• examples/, idf_component_examples/, pioarduino_examples/: usage samples for Arduino, ESP-IDF, PlatformIO.
• library.json / library.properties: Arduino registry metadata.
README insights
• Confirms it’s a library—no standalone app—built on top of an asynchronous TCP layer (AsyncTCP, AsyncTCPSock, or RPAsyncTCP).
• Lists features (WebSocket, SSE, file serving, rewrite, redirect, authentication, JSON, upload).
• Documents build flags and runtime tuning options for the underlying TCP queue, stack size, core affinity.
a. Main components
• Hardware platform (microcontroller / Wi-Fi chipset)
• Network stack / RTOS (LWIP/TCP, FreeRTOS tasks)
• AsyncTCP abstraction (platform-specific TCP driver)
• ESPAsyncWebServer core library modules:
– AsyncWebServer (main server manager)
– AsyncWebSocket / AsyncEventSource (real-time channels)
– Middleware pipeline (filters, CORS, auth)
– Router & Handlers (URL matching, static file, chunked response)
– Request / Response objects (parsing, headers, lifecycle)
– JSON & MessagePack serializers
– WebAuthentication module
• User application code (callbacks, business logic)
• Optional storage (SPIFFS/LittleFS for static files)
• Examples harness (demonstrates API usage)
b. Relationships & data flow
1. Client (browser or IoT client) → TCP/IP over Wi-Fi → AsyncTCP driver
2. AsyncTCP passes raw data → AsyncWebServer’s event-driven loop
3. Incoming HTTP request → parsed into WebRequest → routed through Middleware → matched to a WebHandler
4. WebHandler invokes user callback → builds WebResponse (static file, chunk, JSON, SSE, WebSocket frame)
5. WebResponse → formatted into HTTP frames → AsyncTCP sends over network
6. For long-lived channels: AsyncWebSocket / AsyncEventSource maintain state; push data asynchronously on events.
c. Architectural patterns & design principles
• Event-driven, callback-based non-blocking I/O
• Middleware pipeline for cross-cutting concerns (auth, CORS, logging)
• Modular separation: transport (TCP) vs application (HTTP, WebSocket) layers
• Pluggable serializers (ArduinoJson, MessagePack)
• Build-time configuration via CMake / PlatformIO flags
d. Key technologies & dependencies
• CMake / PlatformIO build system
• ESP-IDF or Arduino core for ESP32/ESP8266/RP2040
• AsyncTCP / AsyncTCPSock / RPAsyncTCP for non-blocking TCP
• ArduinoJson (v7) for JSON support
• FreeRTOS (task affinity, queue size tuning)
• SPIFFS / LittleFS (optional) for static file storage