Skip to the content.

ESP Async WebServer

License: LGPL 3.0 Continuous Integration PlatformIO Registry

Asynchronous HTTP and WebSocket Server Library for ESP32, ESP8266 Supports: WebSocket, SSE, Authentication, Arduino Json 7, File Upload, Static File serving, URL Rewrite, URL Redirect, etc.

This fork is based on yubox-node-org/ESPAsyncWebServer and includes all the concurrency fixes.

Changes in this fork

Documentation

Usage and API stays the same as the original library. Please look at the original libraries for more examples and documentation.

AsyncWebSocketMessageBuffer and makeBuffer()

The fork from yubox-node-org introduces some breaking API changes compared to the original library, especially regarding the use of std::shared_ptr<std::vector<uint8_t>> for WebSocket.

This fork is compatible with the original library from me-no-dev regarding WebSocket, and wraps the optimizations done by yubox-node-org in the AsyncWebSocketMessageBuffer class. So you have the choice of which API to use.

Here are examples for serializing a Json document in a websocket message buffer:

void send(JsonDocument& doc) {
  const size_t len = measureJson(doc);

  // original API from me-no-dev
  AsyncWebSocketMessageBuffer* buffer = _ws->makeBuffer(len);
  assert(buffer); // up to you to keep or remove this
  serializeJson(doc, buffer->get(), len);
  _ws->textAll(buffer);
}
void send(JsonDocument& doc) {
  const size_t len = measureJson(doc);

  // this fork (originally from yubox-node-org), uses another API with shared pointer
  auto buffer = std::make_shared<std::vector<uint8_t>>(len);
  assert(buffer); // up to you to keep or remove this
  serializeJson(doc, buffer->data(), len);
  _ws->textAll(std::move(buffer));
}

I recommend to use the official API AsyncWebSocketMessageBuffer to retain further compatibility.

Stack size and queues

Here are some important flags to tweak depending on your needs:

  // Async TCP queue size
  -D CONFIG_ASYNC_TCP_QUEUE_SIZE=128
  // Async TCP async task core
  -D CONFIG_ASYNC_TCP_RUNNING_CORE=1
  // Async TCP async stac ksize
  -D CONFIG_ASYNC_TCP_STACK_SIZE=8096
  // WebSocket queue size
  -D WS_MAX_QUEUED_MESSAGES=64