A specialized port of the IXWebSocket library specifically tailored for the ESP-IDF v5.x framework (ESP32 series).
This project provides a robust C++ WebSocket and HTTP client/server for ESP32 chips. It has been modified to handle the specific networking stack (LWIP) and security requirements (MbedTLS 3.x) of the latest Espressif IoT Development Framework.
This project is a modified version of the original IXWebSocket library by Machine Zone, Inc. All credit for the core logic and architecture goes to the original authors. This port adds the necessary compatibility layers for ESP-IDF integration.
- Full HTTP Client: Support for GET, POST, and other methods, making it a complete networking replacement for standard ESP-IDF components.
- MbedTLS 3.x Support: Updated
mbedtls_pk_parse_keyfilecalls to match the new API requirements (added RNG parameters). - POSIX Compatibility: Added a
compatlayer for missing headers likenetinet/ip.h,netinet/tcp.h, andpoll.h. - LWIP Integration: Replaced
gai_strerrorwith ESP-friendly error reporting inIXDNSLookup.cpp. - Interrupt Mechanism: Switched from
pipe()(unsupported in IDF) toEventFDviaIXSelectInterruptEvent.cpp. - Linker Fixes: Explicitly defined
DNSLookup::kDefaultWaitto prevent "undefined reference" errors.
Add this to your project's idf_component.yml file:
dependencies:
YauheniMarchanka/esp-ixwebsocket: "^1.0.0"Clone this repository into your project's components directory:
mkdir -p components
cd components
git clone https://github.com/YauheniMarchanka/esp-ixwebsocket.git IXWebSocket#include <ixwebsocket/IXWebSocket.h>
void setup_websocket() {
ix::WebSocket webSocket;
webSocket.setUrl("ws://echo.websocket.org");
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg) {
if (msg->type == ix::WebSocketMessageType::Message) {
printf("Received: %s\n", msg->str.c_str());
} else if (msg->type == ix::WebSocketMessageType::Error) {
printf("Error: %s\n", msg->errorInfo.reason.c_str());
}
});
webSocket.start();
}IXWebSocket allows you to handle complex HTTP tasks without needing additional libraries like esp_http_client.
#include <ixwebsocket/IXHttpClient.h>
void http_get_example() {
ix::HttpClient httpClient;
auto args = httpClient.createRequest();
// Set a timeout or custom headers if needed
args->timeout = 10;
std::string url = "http://httpbin.org/get";
auto response = httpClient.get(url, args);
if (response->errorCode == ix::HttpErrorCode::Ok) {
printf("HTTP Status: %d\n", response->statusCode);
printf("Body: %s\n", response->body.c_str());
} else {
printf("HTTP Error: %s\n", response->errorMsg.c_str());
}
}To ensure stable operation on ESP32, please check the following settings in your sdkconfig (via idf.py menuconfig):
- Stack Size: C++ and TLS operations require significant stack space.
- Increase
CONFIG_ESP_MAIN_TASK_STACK_SIZEto at least10240(10KB). - If using in a sub-task, ensure
xTaskCreateuses a similar stack size.
- Increase
- EventFD: Ensure
CONFIG_LWIP_POSIX_INTERNAL_EVENT_WAKEUPis enabled (usually ON by default) for the interrupt mechanism to work correctly. - MbedTLS: For SSL/TLS, ensure
CONFIG_MBEDTLS_DYNAMIC_BUFFER_MANAGEMENTis enabled to save RAM.
This port is distributed under the BSD 3-Clause License, the same as the original IXWebSocket library. See the LICENSE.txt file for details.