Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:78 Xiaozhi esp32 Protocol Transport Abstraction

From Leeroopedia
Knowledge Sources
Domains Network_Protocols, Software_Architecture
Last Updated 2026-10-17 00:00 GMT

Overview

A transport-neutral protocol abstraction that defines a common API for bidirectional audio streaming and JSON messaging, enabling a voice assistant to communicate over WebSocket or MQTT/UDP transports without the core logic knowing which transport is active.

Description

The Protocol Transport Abstraction is a Strategy pattern applied to network transports. The Protocol base class defines the interface for:

  • Connection lifecycle: Start(), OpenAudioChannel(), CloseAudioChannel()
  • Audio streaming: SendAudio(std::unique_ptr<AudioStreamPacket>)
  • JSON messaging: SendWakeWordDetected(), SendStartListening(), SendStopListening(), SendAbortSpeaking(), SendMcpMessage()
  • Callbacks: OnIncomingJson(), OnIncomingAudio(), OnAudioChannelOpened(), OnAudioChannelClosed(), OnNetworkError()

Two concrete implementations:

  • WebsocketProtocol: Uses a persistent WebSocket connection. Audio is sent as binary frames using a versioned binary protocol (v1=raw Opus, v2=BinaryProtocol2 with timestamp, v3=BinaryProtocol3 compact). JSON is sent as text frames.
  • MqttProtocol: Uses MQTT for JSON control messages and UDP for audio data (lower latency than MQTT for binary). Audio is AES-CTR encrypted. Handles reconnection with a 60s reconnect timer.

Both implementations share the same JSON message semantics (defined in Protocol base), so the server can use either transport. The base class also manages server_sample_rate_, server_frame_duration_, and session_id_ which are set from the server's hello message.

Usage

Use this principle when:

  • A device must support multiple network transports (WebSocket for WiFi, MQTT/UDP for cellular)
  • Core logic should not be coupled to transport-specific framing or connection management
  • Shared message semantics must be maintained across transports
  • The server may assign different transport endpoints at activation time

Theoretical Basis

The Strategy pattern with a shared semantic layer:

                    Protocol (base)
                   /              \
          WebsocketProtocol    MqttProtocol
          (WebSocket)           (MQTT + UDP)

Shared semantics (in Protocol base):
  JSON: {"type":"listen","state":"start","mode":"auto"}
  JSON: {"type":"listen","state":"detect","text":"..."}
  JSON: {"type":"abort","reason":"wake_word_detected"}
  Audio: AudioStreamPacket {sample_rate, frame_duration, timestamp, payload}

Transport-specific:
  WebSocket: binary frame with BinaryProtocol2/3 header
  MQTT/UDP:  AES-CTR encrypted UDP datagram

The server hello handshake negotiates transport parameters:

Device → Server: hello JSON (device info, capabilities)
Server → Device: hello JSON (sample_rate, frame_duration, session_id, version)
  → server_sample_rate_ = 24000
  → server_frame_duration_ = 60
  → session_id_ assigned
  → version_ determines binary protocol format

Practical Guide

When implementing transport abstraction:

  • Keep message semantics in the base class: SendStartListening(), SendStopListening(), etc. construct the same JSON regardless of transport.
  • Let subclasses handle framing: SendText() and SendAudio() are pure virtual; each transport serializes differently.
  • Negotiate parameters at channel open: the hello exchange sets sample rate, frame duration, and session ID.
  • Handle reconnection: MQTT uses a timer-based reconnect; WebSocket relies on the underlying socket library.
  • Verify both transports when changing shared contracts: any change to JSON message format must work on both WebSocket and MQTT.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment