BlazeSub is a high-performance, lock-free publish/subscribe system designed to outperform traditional MQTT brokers. It provides efficient message routing with support for wildcard subscriptions while maintaining thread safety through lock-free data structures.
- β‘ Ultra-fast performance: Up to 84.7 million messages per second delivered to 1000 subscribers
- π§ Zero memory allocations: Core operations don't allocate memory, reducing GC pressure
- π Thread-safe by design: Uses lock-free data structures for maximum concurrency
- π³ MQTT-compatible topic matching: Supports single-level (+) and multi-level (#) wildcards
- π Efficient topic caching: Optimizes repeat accesses to common topics
- π Flexible message delivery: Choose between worker pool or direct goroutines for optimal performance
- β±οΈ Low-latency message delivery: Direct goroutines up to 52% faster than worker pool and 34% faster than MQTT
- π¦ Rich metadata support: Attach arbitrary metadata to messages for enhanced application context
- π§© Generic message types: Define your own message data types without serialization/deserialization overhead
- π― Direct match throughput: 84.7 million messages per second to 1000 subscribers
- π Wildcard match throughput: 83.5 million messages per second to 1000 subscribers
- π Memory efficiency: Uses up to 95% less memory than MochiMQTT
- 0οΈβ£ Zero allocations for core subscription matching operations
- ποΈ Minimal GC impact: Only 2 allocations per publish operation
- API Reference - Complete API documentation on pkg.go.dev
- User Guide - Comprehensive guide for using BlazeSub
- Performance Analysis - Detailed performance metrics and comparisons
- MaxConcurrentSubscriptions Guide - Optimizing message delivery to multiple subscribers
BlazeSub supports both exact and wildcard subscriptions, following the MQTT topic matching rules:
- Match topics exactly as specified
- Example:
sensors/temperatureonly matches messages published tosensors/temperature
BlazeSub supports two types of wildcards:
- Matches exactly one level in the topic hierarchy
- Can be used multiple times in a topic
- Examples:
sensors/+/temperaturematches:sensors/room1/temperaturesensors/room2/temperature- But not
sensors/temperatureorsensors/room1/floor1/temperature
+/+/temperaturematches any topic with exactly three levels ending intemperature
- Matches any number of levels in the topic hierarchy
- Must be the last character in the topic
- Can only be used once
- Examples:
sensors/#matches:sensors/temperaturesensors/humiditysensors/room1/temperature- Any topic starting with
sensors/
#matches all topics
// Exact match
bus.Subscribe("sensors/temperature")
// Single-level wildcard
bus.Subscribe("sensors/+/temperature") // Matches any single level
bus.Subscribe("+/+/temperature") // Matches exactly three levels
// Multi-level wildcard
bus.Subscribe("sensors/#") // Matches all sensors topics
bus.Subscribe("#") // Matches all topics// Create a new bus with defaults ([]byte for the message)
bus, err := blazesub.NewBusWithDefaults()
if err != nil {
log.Fatal(err)
}
defer bus.Close()
// Subscribe to a topic
subscription, err := bus.Subscribe("sensors/temperature")
if err != nil {
log.Fatal(err)
}
// Handle messages
subscription.OnMessage(blazesub.MessageHandlerFunc[[]byte](func(msg *blazesub.Message[[]byte]) error {
fmt.Printf("Received: %s\n", string(msg.Data))
// Access metadata (if any)
if msg.Metadata != nil {
if unit, ok := msg.Metadata["unit"].(string); ok {
fmt.Printf("Unit: %s\n", unit)
}
}
return nil
}))
// Publish to a topic (with metadata)
metadata := map[string]any{
"unit": "celsius",
"device_id": "thermostat-living-room",
}
bus.Publish("sensors/temperature", []byte("25.5"), metadata)
// When done
subscription.Unsubscribe()// Define your custom message type
type SensorReading struct {
Value float64 `json:"value"`
Unit string `json:"unit"`
DeviceID string `json:"device_id"`
Timestamp time.Time `json:"timestamp"`
}
// Create a bus with your custom type
bus, err := blazesub.NewBusWithDefaultsOf[SensorReading]()
if err != nil {
log.Fatal(err)
}
defer bus.Close()
// Subscribe and handle typed messages directly
subscription, err := bus.Subscribe("sensors/temperature")
if err != nil {
log.Fatal(err)
}
subscription.OnMessage(blazesub.MessageHandlerFunc[SensorReading](func(msg *blazesub.Message[SensorReading]) error {
// Direct access to structured data!
reading := msg.Data
fmt.Printf("Device %s reports %.1f %s at %s\n",
reading.DeviceID, reading.Value, reading.Unit, reading.Timestamp.Format(time.RFC3339))
return nil
}))
// Publish structured data directly
bus.Publish("sensors/temperature", SensorReading{
Value: 22.5,
Unit: "celsius",
DeviceID: "thermostat-living-room",
Timestamp: time.Now(),
})For optimal performance, consider using direct goroutines:
config := blazesub.Config{
UseGoroutinePool: false, // Use direct goroutines for max performance
MaxConcurrentSubscriptions: 50, // Optimal for most workloads
}
bus, err := blazesub.NewBus(config)For more examples demonstrating different configurations and usage patterns, see the Examples section on pkg.go.dev.
| Option | Purpose | Recommendation |
|---|---|---|
| UseGoroutinePool | Delivery mode | false for speed, true for resource control |
| MaxConcurrentSubscriptions | Delivery batching | Keep below your subscriber count |
See the User Guide for detailed configuration information.
BlazeSub uses GoReleaser for streamlined releases. To create a new release:
- Update Version: Update documentation and code with the new version
- Create Tag:
git tag -a v0.x.y -m "Release v0.x.y" git push origin v0.x.y - Automatic Release: GitHub Actions will automatically:
- Build and validate the release
- Generate documentation archive
- Create GitHub release with release notes
- Update pkg.go.dev
MIT License
Copyright (c) 2023 BlazeSub Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
