Illustrative voice terminal and café network environment, not a particular product or test site
Embedded System Development

Choosing an ESP32 Voice Transport: WebRTC, WebSocket, or MQTT + UDP

Compare WebRTC, WebSocket, and MQTT + UDP for ESP32 voice devices through server compatibility, audio freshness, network reachability, and media-stack ownership.

ESP32
Implementation bridge

Using this research in a live engineering project?

Send the device, workflow, data, integration, or deployment constraints. An engineer can help turn the article direction into a scoped next step.

The first useful question when choosing an ESP32 voice transport belongs at the server: does it accept WebSocket audio with JSON events, a particular MQTT-control/UDP-media protocol, or a WebRTC session? If the existing service offers only one of those interfaces, changing the device transport adds a server-adaptation project. It is more than a client-library swap.

Keep WebSocket on the shortlist when a compatible voice service already works, the device initiates connectivity, and the interaction can tolerate a defined queue-management policy. Consider MQTT + UDP when there is an agreed two-channel device protocol and a team willing to operate its media endpoint. Start with WebRTC when the required peer is a browser or an RTC service. These routes carry different integration responsibilities; none earns a universal low-latency crown independently of its service and network.

What does the server mean by “supported”?

A successful WebSocket connection says little about the messages the server expects. An ESP32 might send binary Opus frames while the service waits for event-wrapped PCM. The connection is healthy, but neither side can use the other's audio. Authentication, session startup, uplink and downlink formats, errors, and closing behavior all need to match.

The pinned Xiaozhi WebSocket protocol, for example, distinguishes JSON text events from binary Opus audio on one connection and exchanges audio parameters in its hello messages. That gives a self-hosted implementation a concrete contract. An arbitrary ASR or language-model WebSocket API still needs its own format and framing comparison; the shared transport name does not make the interfaces interchangeable.

Staying with an existing compatible device server can preserve its authentication, routing, and operational tooling. Adding a gateway to reach a WebRTC-only service changes that calculation. The gateway must establish sessions on both sides, translate media and events, and track compatibility as the service evolves. Simplifying device code can be a reasonable choice, but somebody inherits the other half of the system.

The three candidates also sit at different levels. WebSocket carries bidirectional messages. MQTT + UDP here means an application-specific separation of control and media. WebRTC supplies a collection of media, negotiation, and connection-establishment capabilities. Treating them as interchangeable audio plugs hides server work that may dominate the choice.

Existing condition Candidate Work that stays with the choice
A WebSocket voice service matching the device message format WebSocket Audio framing, send queues, and new-session handling after reconnection
Matching versions of an MQTT control contract and UDP media service MQTT + UDP Channel association, media reachability, keys, and stale-audio handling
A target service or browser peer requiring WebRTC WebRTC Signaling adaptation, codecs, ICE/relay capabilities, and embedded media components

Service policy can change without changing the underlying protocol. In Xiaozhi issue #1188, a maintainer gave advice restricting WebSocket use with the official service on September 11, 2025. That is dated guidance about a particular service, not a reason to abandon every self-hosted WebSocket deployment in 2026. Confirm the actual endpoint and version the product intends to use.

Complete audio can still be too old

RFC 6455 defines WebSocket over TCP. Reliable delivery in order is useful when every piece of text or file data matters. Continuous audio has an additional question: is a frame still worth playing when it arrives?

TCP's specification, RFC 9293, describes a reliable, ordered byte stream with retransmission. Missing earlier bytes can hold up later data while recovery takes place. In a voice application, the result can be intact audio that now trails the conversation. This mechanism explains what to investigate; it supplies no measured latency figure for a particular ESP32.

Translate a queue into audio age, not only bytes. Five queued frames representing 60 ms each contain 300 ms of sound. That is arithmetic using an assumed frame duration, not a device result. Compression changes the byte count without making that duration disappear. Use the actual negotiated frame length rather than treating a documentation example as a universal setting.

For WebSocket, the design work is therefore concrete: bound send sizes and application queues, and decide whether congestion pauses capture, discards old frames not yet sent, or ends the connection to start afresh. Deleting an application queue cannot guarantee withdrawal of bytes already handed to TCP. Short utterances, push-to-talk interaction, or known network conditions may make these trade-offs acceptable. Sustained conversations need an explicit policy for limiting accumulated audio.

UDP's base specification does not guarantee delivery or duplicate protection. Independent datagrams let an application decide which audio has become too old to wait for. They also leave sequence handling, playout timing, reordering, and missing-frame policy to the media implementation. Bare UDP supplies neither a complete voice-quality system nor default authentication, encryption, or replay protection.

Accepting occasional loss is different from dropping arbitrary frames. The receiver needs a playout schedule, a decoder policy for gaps, and a decision about late data. Less waiting may mean an audible gap; a large buffer can erase the intended freshness benefit. A team that needs this control has a reason to take on the additional media code and observability. A protocol label alone is not that reason.

MQTT connectivity covers only the control channel

The pinned Xiaozhi MQTT + UDP documentation provides a specific example. MQTT carries control and status, a hello reply supplies a UDP address, port, and encryption parameters, and audio uses the separate UDP channel. Those are application-protocol decisions, not media behavior defined by MQTT for every voice product.

Splitting the paths also splits their failure conditions. A device may receive MQTT status messages while UDP audio cannot leave the network. A broker heartbeat does not establish that playback continues. After MQTT reconnects, whether the old media endpoint and parameters remain usable depends on the application's renegotiation rules. Restored control connectivity is not a complete voice-session recovery signal.

This implementation describes AES-CTR audio encryption and sequence handling; neither comes from UDP itself. A product team adopting it still needs to examine control-channel protection, media integrity and identity binding, and key lifetime separately. The word “encrypted” does not answer how altered data or replayed packets are handled. An unchecked private frame format should not become a general-purpose security interface by assumption.

A controlled LAN with an existing MQTT device system and a team operating the media service gives the split-channel approach a clear place. Device status can remain in the existing control system while the media endpoint manages audio freshness. Owning a broker but no audio service offers much less of a head start. Allocating a UDP port does not provide decoding, transcription, or model conversation.

Router and Ethernet cabling in a small commercial network cabinet

Illustrative network deployment scene, not a particular firewall configuration, wiring plan, or call test.

WebRTC brings a media system to the device

RFC 8835 describes a WebRTC protocol suite and addresses NAT, firewall, and relay conditions. That makes it relevant to systems needing RTC interoperability. On an ESP32, however, the selected library's actual capabilities still matter. Browser requirements cannot be copied into an embedded component's support matrix, much less presented as target-board validation.

Espressif's ESP-WebRTC component documentation at a pinned commit separates signaling, PeerConnection, and the media system, with interfaces for customized signaling. This distinction changes the comparison: WebRTC signaling can use WebSocket while audio travels over the negotiated media connection. Finding a WebSocket URL in a project does not establish that it streams its audio over WebSocket.

Browser participation, cross-network connections, or integration with existing RTC peers can justify investing in ICE and relay operations. A single device talking only to its own voice service may gain less from the porting and dependency work of a fuller media stack. Relays also bring bandwidth and operating costs. Estimate those from topology and actual use; no protocol name supplies a cost figure.

Choose the example as carefully as the library. Espressif's peer_demo explicitly uses simulated audio. It helps explain connection APIs without establishing that a real microphone, speaker, and acoustic path work. The openai_demo is a different integration entry point with capture and playback. Selecting it still requires fixing the board, component, and service-interface versions. A README build path is not this team's board-test result.

Capture, codecs, transport, and playback all need buffers and task scheduling on the device. A more complete media library can provide reusable mechanisms while adding configuration and dependencies to understand. Without peak-memory, CPU-load, and sustained-operation measurements under the same audio workload, there is no basis for declaring one route lighter on the current board. The comparison here concerns responsibilities and constraints, not a resource ranking.

Move the recommendation when the product changes

Consider a desktop terminal with an existing private WebSocket conversation service. If its device format matches and the application can accept a defined backlog policy, retaining the route has a clear rationale. Adding a media gateway merely to adopt a more “real-time” name introduces deployment, conversion, and fault-location work. The benefit should come from a concrete new requirement.

Now consider a device fleet already managed over MQTT, with a team planning its own media endpoint. Separating control from media creates room to manage freshness independently, but operations must see both paths. If a deployment network disallows the required UDP traffic, the options include agreeing a different network policy, selecting a reachable interface, or adding a supported relay. Repeated firmware reconnects cannot remove the restriction.

A device required to interoperate with a browser or existing RTC service starts from a different position. Investigating the matching WebRTC library and service contract is more useful than rebuilding a media interoperability layer. Where UDP is restricted, check the relay modes supported by that embedded stack and service. “WebRTC traverses networks” is not a guarantee against every network policy, and does not imply automatic fallback to an arbitrary WebSocket audio protocol.

None of these product conditions is a higher tier. The server's available interface, the site's permitted network paths, and the media responsibilities the team will maintain should narrow the choice. What remains should be a route whose suitability can be explained for this product—not a podium placing WebRTC, WebSocket, and UDP first, second, and third.

For the broader division between device, conversation service, and product backend, see the ESP32 voice-terminal architecture guide. For target-board and private-service integration, see ESP32 development services. This comparison uses pinned source material and public specifications checked on November 23, 2026; it includes no target-board calling or weak-network measurements.