Chapter 2. API Reference
Application Example
A simple WebSocket example that uses esp_websocket_client to establish a websocket connection and send/receive
data with the websocket.org server can be found here: protocols/websocket.
Sending Text Data The WebSocket client supports sending data as a text data frame, which informs the application
layer that the payload data is text data encoded as UTF-8. Example:
esp_websocket_client_send_text(client, data, len, portMAX_DELAY);
API Reference
Header File
• components/esp_websocket_client/include/esp_websocket_client.h
Functions
esp_websocket_client_handle_t esp_websocket_client_init(const esp_websocket_client_config_t
*config)
Start a Websocket session This function must be the first function to call, and it returns a
esp_websocket_client_handle_t that you must use as input to other functions in the interface. This call MUST
have a corresponding call to esp_websocket_client_destroy when the operation is complete.
Return
• esp_websocket_client_handle_t
• NULL if any errors
Parameters
• [in] config: The configuration
esp_err_t esp_websocket_client_set_uri(esp_websocket_client_handle_t client, const char
*uri)
Set URL for client, when performing this behavior, the options in the URL will replace the old ones Must stop
the WebSocket client before set URI if the client has been connected.
Return esp_err_t
Parameters
• [in] client: The client
• [in] uri: The uri
esp_err_t esp_websocket_client_start(esp_websocket_client_handle_t client)
Open the WebSocket connection.
Return esp_err_t
Parameters
• [in] client: The client
esp_err_t esp_websocket_client_stop(esp_websocket_client_handle_t client)
Stops the WebSocket connection without websocket closing handshake.
This API stops ws client and closes TCP connection directly without sending close frames. It is a good practice
to close the connection in a clean way using esp_websocket_client_close().
Notes:
• Cannot be called from the websocket event handler
Return esp_err_t
Parameters
• [in] client: The client
esp_err_t esp_websocket_client_destroy(esp_websocket_client_handle_t client)
Destroy the WebSocket connection and free all resources. This function must be the last function to call for an
Espressif Systems 644
Submit Document Feedback
Release v4.4