Try It: AMQP Message Exchange Simulator on ESP32
Try It: AMQP Message Exchange Simulator on ESP32
Technical boundaries. Routing uses simulated queues and simple pattern matching. Consumer crashes, acknowledgments, and prefetch batches are printed demonstrations.
Objective: Simulate AMQP exchange types (direct, fanout, topic) and message routing on ESP32, demonstrating how producers publish to exchanges, routing keys determine queue delivery, and consumers acknowledge messages – the same patterns used in production RabbitMQ deployments.
Paste this code into the Wokwi editor:
#include <WiFi.h>
// Simulated AMQP components
struct Message {
const char* routingKey;
const char* body;
int deliveryMode; // 1=transient, 2=persistent
bool acked;
};
struct Queue {
const char* name;
const char* bindingKey;
bool durable;
int messageCount;
int ackedCount;
int nackedCount;
};
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("=== AMQP Exchange Routing Simulator ===\n");
// === 1. Direct Exchange ===
Serial.println("--- Direct Exchange: 'sensor-alerts' ---");
Serial.println("Routing: exact match between routing key and binding key\n");
Queue directQueues[] = {
{"temp-critical", "alert.temperature.critical", true, 0, 0, 0},
{"humidity-warn", "alert.humidity.warning", true, 0, 0, 0},
{"all-alerts", "alert.temperature.critical", true, 0, 0, 0}
};
Message directMsgs[] = {
{"alert.temperature.critical", "{\"temp\":85,\"unit\":\"C\"}", 2, false},
{"alert.humidity.warning", "{\"humidity\":92,\"unit\":\"%\"}", 2, false},
{"alert.pressure.info", "{\"pressure\":1013,\"unit\":\"hPa\"}", 1, false}
};
for (int m = 0; m < 3; m++) {
Serial.printf("PUBLISH key='%s' | persistent=%s\n",
directMsgs[m].routingKey,
directMsgs[m].deliveryMode == 2 ? "yes" : "no");
bool routed = false;
for (int q = 0; q < 3; q++) {
if (strcmp(directMsgs[m].routingKey, directQueues[q].bindingKey) == 0) {
directQueues[q].messageCount++;
routed = true;
Serial.printf(" -> Delivered to '%s' (depth: %d)\n",
directQueues[q].name, directQueues[q].messageCount);
}
}
if (!routed) {
Serial.println(" -> UNROUTABLE (no matching binding) - message DROPPED!");
Serial.println(" FIX: Configure alternate-exchange or mandatory flag");
}
}
// === 2. Fanout Exchange ===
Serial.println("\n--- Fanout Exchange: 'system-broadcast' ---");
Serial.println("Routing: all bound queues receive every message\n");
const char* fanoutQueues[] = {"logging", "analytics", "backup"};
Serial.println("PUBLISH key='ignored' body='{\"event\":\"system.restart\"}'");
for (int q = 0; q < 3; q++) {
Serial.printf(" -> Delivered to '%s' (fanout ignores routing key)\n",
fanoutQueues[q]);
}
// === 3. Topic Exchange ===
Serial.println("\n--- Topic Exchange: 'iot-telemetry' ---");
Serial.println("Routing: pattern matching (* = one word, # = zero or more)\n");
struct TopicBinding {
const char* queue;
const char* pattern;
};
TopicBinding topicBindings[] = {
{"floor1-all", "factory.floor1.*"},
{"all-temperature", "factory.*.temperature"},
{"everything", "factory.#"},
{"emergency", "factory.*.emergency"}
};
const char* topicMsgs[] = {
"factory.floor1.temperature",
"factory.floor1.humidity",
"factory.floor2.temperature",
"factory.floor1.emergency",
"factory.floor2.line3.vibration"
};
for (int m = 0; m < 5; m++) {
Serial.printf("PUBLISH key='%s'\n", topicMsgs[m]);
for (int b = 0; b < 4; b++) {
bool match = false;
// Simple pattern matching simulation
String key = topicMsgs[m];
String pattern = topicBindings[b].pattern;
if (pattern.endsWith("#")) {
String prefix = pattern.substring(0, pattern.length() - 1);
match = key.startsWith(prefix);
} else if (pattern.indexOf('*') >= 0) {
// Count dots to check word count matches
int keyDots = 0, patDots = 0;
for (int i = 0; i < key.length(); i++) if (key[i] == '.') keyDots++;
for (int i = 0; i < pattern.length(); i++) if (pattern[i] == '.') patDots++;
if (keyDots == patDots) {
// Check non-wildcard segments
match = true;
int ki = 0, pi = 0;
while (ki < key.length() && pi < pattern.length()) {
if (pattern[pi] == '*') {
while (ki < key.length() && key[ki] != '.') ki++;
while (pi < pattern.length() && pattern[pi] != '.') pi++;
} else if (key[ki] == pattern[pi]) {
ki++; pi++;
} else {
match = false; break;
}
}
}
} else {
match = (key == pattern);
}
if (match) {
Serial.printf(" -> Matched '%s' (pattern: %s)\n",
topicBindings[b].queue, topicBindings[b].pattern);
}
}
}
// === 4. Consumer Acknowledgment Demo ===
Serial.println("\n--- Consumer Acknowledgment Patterns ---\n");
Serial.println("Auto-ack (DANGEROUS for critical data):");
Serial.println(" Consumer receives msg -> ACK sent immediately");
Serial.println(" Consumer CRASHES during processing -> MESSAGE LOST!\n");
Serial.println("Manual-ack (SAFE for production):");
Serial.println(" Consumer receives msg -> Processes msg -> Sends ACK");
Serial.println(" Consumer CRASHES during processing -> Msg REDELIVERED\n");
Serial.println("Prefetch=10 with manual ack:");
for (int i = 1; i <= 10; i++) {
Serial.printf(" [%02d] Received -> Processing... -> ACK\n", i);
}
Serial.println(" Broker delivers next batch of 10\n");
Serial.println("=== AMQP Exchange Routing Demo Complete ===");
}
void loop() {
delay(10000);
}What to Observe:
First: Direct exchange routes only to queues with an exact routing key match – the “pressure.info” message is dropped because no queue binds that key (a common production data loss scenario)
Next: Topic exchange patterns: factory.floor1.* matches exactly 3-word keys starting with factory.floor1, while factory.# matches ALL messages (including the 5-word factory.floor2.line3.vibration)
Then: Fanout exchange ignores routing keys entirely – every bound queue gets every message, perfect for logging and analytics
After that: Auto-ack vs manual-ack is the #1 cause of production data loss – auto-ack acknowledges before processing, so a crash loses the message permanently