Websocket Server
Overview
The Orchestrator device uses WebSocket to exchange messages with connected clients.
This section outlines the development of the Websocket Server component.
Development
Bootstrap Component
The command below creates a new component named websocket_server inside the components directory.
Change the file extension of the websocket_server.c file to websocket_server.cpp and move it to the src folder.
Enable WebSocket Support by setting the CONFIG_HTTPD_WS_SUPPORT option to y is the sdkconfig.defaults file.
WebSocket Secure (WSS)
The server uses ESP-IDF's esp_https_server to handle secure WebSocket (WSS) connections over HTTPS.
A private Root CA signs the Orchestrator’s certificate, which includes its IP address in the SAN field. The client has the Root CA certificate to verify the Orchestrator during TLS handshake.
Clients connect to /ws. The server performs the WebSocket handshake and upgrades the connection.
The server handles incoming WebSocket frames:
TEXTechoed backPINGreplies with PONGCLOSEcloses the connection gracefully
Outgoing messages are sent asynchronously using ESP-IDF's work queue.
TLS Certificates
This project requires locally generated certificates to establish a WSS connection between the Orchestrator and a client application.
First-time Setup (after cloning):
Generating Certificates:
Keep-Alive Mechanism
The keep-alive mechanism automatically monitors active WebSocket clients to detect and remove dead connections. It sends periodic PING messages to each client and waits for a PONG response. If a client fails to respond within the configured timeout, it is marked as inactive and disconnected.
The module tracks clients by their socket file descriptors and runs as a separate FreeRTOS task, using a queue to handle client add, remove, and update events.
Message Handling
Incoming messages are handled by a registered JSON parser.