Swift code examples for the RabbitMQ tutorials.
These tutorials use BunnySwift, a modern Swift 6 RabbitMQ client with async/await support.
- Swift 6.0 or later
- macOS 14+ (or iOS 17+, tvOS 17+, watchOS 10+, visionOS 1+)
- A running RabbitMQ server on localhost
Build all tutorials:
swift buildIn one terminal, start the receiver:
swift run ReceiveIn another terminal, send a message:
swift run SendStart one or more workers:
swift run WorkerSend tasks with varying workloads (dots indicate seconds of work):
swift run NewTask "A simple task."
swift run NewTask "A longer task..."
swift run NewTask "A very long task....."Start one or more log receivers:
swift run ReceiveLogsEmit log messages:
swift run EmitLog "Hello subscribers!"Subscribe to specific severity levels:
swift run ReceiveLogsDirect error warningEmit logs with severity:
swift run EmitLogDirect info "Just an info message"
swift run EmitLogDirect warning "This is a warning"
swift run EmitLogDirect error "This is an error!"Subscribe using topic patterns (* matches one word, # matches zero or more):
swift run ReceiveLogsTopic "kern.*"
swift run ReceiveLogsTopic "*.critical"
swift run ReceiveLogsTopic "#"Emit logs with topic routing keys:
swift run EmitLogTopic "kern.info" "Kernel info"
swift run EmitLogTopic "kern.critical" "Kernel critical error"
swift run EmitLogTopic "auth.critical" "Authentication failure"| Tutorial | Producer | Consumer |
|---|---|---|
| 1. Hello World | Send | Receive |
| 2. Work Queues | NewTask | Worker |
| 3. Pub/Sub | EmitLog | ReceiveLogs |
| 4. Routing | EmitLogDirect | ReceiveLogsDirect |
| 5. Topics | EmitLogTopic | ReceiveLogsTopic |