Skip to content

Socket Consumption API

When developing components that use network sockets, you must register your socket requirements using the Socket Consumption API. ESPHome uses the registrations to size socket limits (for example CONFIG_LWIP_MAX_SOCKETS on ESP32), and on nRF52 to decide whether the TCP stack is built at all. See Platform Support.

On ESP32 with ESP-IDF, the CONFIG_LWIP_MAX_SOCKETS setting limits how many sockets can be open simultaneously. If this limit is too low, components fail to create sockets with errors like:

E (3555) httpd: httpd_server_init: error in creating msg socket (23)

The Socket Consumption API solves this by having components declare their socket needs during configuration validation. Each platform then configures its network stack from the total needs of all components.

Register socket consumption if your component:

  • Creates listening sockets (TCP/UDP servers)
  • Establishes outbound connections (HTTP clients, MQTT, etc.)
  • Uses multicast sockets (mDNS, discovery protocols)
  • Wraps system services that consume sockets

In your component’s __init__.py, add socket to AUTO_LOAD:

AUTO_LOAD = ["socket"]

This ensures the socket component is automatically loaded without requiring users to add it to their configuration.

Create a validator function that registers your socket needs:

from esphome.components import socket
import esphome.config_validation as cv
def _consume_sockets(config):
"""Register socket needs for this component."""
# Example: 1 listening socket + 2 concurrent client connections
socket.consume_sockets(1, "my_component", socket.SocketType.TCP_LISTEN)(config)
socket.consume_sockets(2, "my_component", socket.SocketType.TCP)(config)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema({
# Your component schema here
}).extend(cv.COMPONENT_SCHEMA),
_consume_sockets, # Register socket usage during validation
)

The socket type defaults to SocketType.TCP. Pass the type that matches what the component opens:

  • SocketType.TCP: outbound TCP connections and accepted TCP clients
  • SocketType.TCP_LISTEN: listening TCP sockets
  • SocketType.UDP: UDP sockets, including multicast

Count all sockets your component uses:

Listening sockets: Each server/listener typically needs 1 socket:

# Web server with one listener
socket.consume_sockets(1, "my_server", socket.SocketType.TCP_LISTEN)

Client connections: Add expected concurrent connections:

# API: 1 listener + 3 typical concurrent clients
socket.consume_sockets(1, "api", socket.SocketType.TCP_LISTEN)
socket.consume_sockets(3, "api", socket.SocketType.TCP)

Multicast sockets: Count IPv4 and IPv6 separately:

# mDNS: separate sockets for IPv4 and IPv6
socket.consume_sockets(2, "mdns", socket.SocketType.UDP)

Multiple instances: Register per-instance if MULTI_CONF is enabled:

def _consume_camera_sockets(config):
# Each camera instance needs 3 sockets (1 listener + 2 clients)
name = f"esp32_camera_web_server_{config[CONF_ID]}"
socket.consume_sockets(1, name, socket.SocketType.TCP_LISTEN)(config)
socket.consume_sockets(2, name, socket.SocketType.TCP)(config)
return config
def _consume_mqtt_sockets(config):
"""MQTT needs one socket for broker connection."""
from esphome.components import socket
socket.consume_sockets(1, "mqtt", socket.SocketType.TCP)(config)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema({
# MQTT configuration
}).extend(cv.COMPONENT_SCHEMA),
_consume_mqtt_sockets,
)

Components that serve requests through the shared web server (web_server_base) do not register the listening socket; web_server_base does. Register the client connections your component keeps open with web_server_base.add_httpd_clients() instead of socket.consume_sockets():

from esphome.components import web_server_base
DOMAIN = "web_server"
def _consume_web_server_sockets(config):
# A browser opens several connections: page resources, the event stream and control requests
web_server_base.add_httpd_clients(5, DOMAIN)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema({
# Web server configuration
}).extend(cv.COMPONENT_SCHEMA),
_consume_web_server_sockets,
)

add_httpd_clients() registers the TCP sockets and adds them to the shared server’s client total. On ESP32 that total sets the ESP-IDF HTTP server’s open socket limit (max_open_sockets) at build time, never below the ESP-IDF default of 7, so a component that keeps long-lived connections (for example a stream) must count them here, or the server runs out of slots and purges the oldest connection. Call add_httpd_clients() during config validation, and do not register the same clients again with socket.consume_sockets().

If the user sets CONFIG_LWIP_MAX_SOCKETS in sdkconfig_options, the limit is capped at that value minus three (the listening socket and the two UDP control sockets), even when that is below 7. For example, a value of 9 allows 6 clients.

web_server_idf registers the two UDP control sockets that the ESP-IDF HTTP server opens, so components never count those.

Short requests that close right away, like a Prometheus scrape, need no registration, but each one holds a client slot while it is served. If registered long-lived connections fill every slot, such a request makes the server purge one of them, so register one more client when short requests must not interrupt them.

def _consume_camera_sockets(config):
"""Each camera web server needs 3 sockets."""
from esphome.components import socket
# Use unique consumer name per instance
name = f"camera_{config[CONF_ID]}"
socket.consume_sockets(1, name, socket.SocketType.TCP_LISTEN)(config)
socket.consume_sockets(2, name, socket.SocketType.TCP)(config)
return config
CONFIG_SCHEMA = cv.All(
cv.Schema({
cv.GenerateID(): cv.declare_id(ESP32Camera),
# Camera configuration
}).extend(cv.COMPONENT_SCHEMA),
_consume_camera_sockets,
)
  1. Registration Phase: During configuration validation, each component calls socket.consume_sockets(count, name, type) to register its socket needs. type is optional and defaults to SocketType.TCP. Clients of the shared web server call web_server_base.add_httpd_clients(count, name) instead, which registers the sockets the same way.

  2. Accumulation: ESPHome stores these registrations in CORE.data[KEY_SOCKET_CONSUMERS], accumulating the total across all components.

  3. Auto-Configuration: Each platform reads the totals and configures its network stack, as listed under Platform Support. On ESP32, _configure_lwip_max_sockets() sets CONFIG_LWIP_MAX_SOCKETS in sdkconfig.

  4. User Overrides: Manual user settings via sdkconfig_options are respected. ESPHome validates that manual values are sufficient and warns if too low.

The Socket Consumption API is currently used by:

  • ESP32 (ESP-IDF framework): sizes CONFIG_LWIP_MAX_SOCKETS.
  • RP2040 and LibreTiny: size the lwIP connection pools, with minimums on top.
  • nRF52 (Zephyr): decides whether Zephyr’s TCP stack is built at all. A build with no registered SocketType.TCP or SocketType.TCP_LISTEN socket (and no async_tcp) leaves TCP out, so an unregistered TCP socket fails at run time.

Other platforms, such as ESP8266, ignore the registrations. Register your sockets anyway: a component that opens sockets must register them, whatever platform you test on.

If a user manually sets CONFIG_LWIP_MAX_SOCKETS too low, ESPHome will issue a warning:

esp32:
framework:
type: esp-idf
sdkconfig_options:
CONFIG_LWIP_MAX_SOCKETS: "8" # Too low!
# Warning: CONFIG_LWIP_MAX_SOCKETS is set to 8 but components need 22 sockets:
# - api: 4
# - web_server: 5
# - web_server_base: 1
# - web_server_idf: 2
# - mdns: 2
# - ota: 1
# - mqtt: 1
# - camera_cam1: 3
# - camera_cam2: 3
  1. Count accurately: Include all sockets your component creates, including hidden ones (like multicast).

  2. Document assumptions: Comment your socket count calculation for maintainability.

  3. Use descriptive names: The consumer name appears in validation warnings, so make it clear what component is using sockets.

  4. Test socket exhaustion: Verify your component handles socket exhaustion gracefully if the limit is insufficient.

  5. Consider scaling: If your component creates variable numbers of sockets based on configuration, calculate dynamically:

    def _consume_sockets(config):
    from esphome.components import socket
    # 1 listener + configured max_clients
    socket.consume_sockets(1, "my_component", socket.SocketType.TCP_LISTEN)(config)
    socket.consume_sockets(config.get(CONF_MAX_CLIENTS, 5), "my_component", socket.SocketType.TCP)(config)
    return config

“error in creating socket (23)” - ENFILE error indicates socket exhaustion. Component isn’t registering socket usage or count is too low.

Component not auto-loading socket - Ensure AUTO_LOAD = ["socket"] or DEPENDENCIES = ["socket"] is set.

Sockets not counted - Verify _consume_sockets is in the cv.All() validator chain and returns the config.

Manual override ignored - User sdkconfig settings take precedence. Check for warnings about insufficient limits.