https://github.com/heyehang/blazewave
High-performance, event-driven WebSocket library,en:https://deepwiki.com/heyehang/blazewave zh:https://zread.ai/heyehang/blazewave
https://github.com/heyehang/blazewave
event-driven go high-performance websocket
Last synced: 5 months ago
JSON representation
High-performance, event-driven WebSocket library,en:https://deepwiki.com/heyehang/blazewave zh:https://zread.ai/heyehang/blazewave
- Host: GitHub
- URL: https://github.com/heyehang/blazewave
- Owner: heyehang
- License: other
- Created: 2025-07-27T00:09:11.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-12-23T11:38:20.000Z (6 months ago)
- Last Synced: 2025-12-24T22:19:08.759Z (6 months ago)
- Topics: event-driven, go, high-performance, websocket
- Language: Go
- Homepage: https://github.com/heyehang/blazewave
- Size: 847 KB
- Stars: 5
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# π₯ BlazeWave
[](https://golang.org)
[](LICENSE.txt)
[](https://github.com/heyehang/blazewave)
> **High-performance, event-driven WebSocket library for Go** β‘
[English](README.md) | [δΈζ](README_zh.md)
BlazeWave is a modern, production-ready WebSocket library inspired by and improved from [nhooyr/websocket](https://github.com/nhooyr/websocket). It provides blazing-fast performance with high-performance operations, smart buffer pooling, and comprehensive event handling.
## β¨ Features
- **π Industry-Leading Performance**: 3.8Γ throughput increase, 2.7Γ latency optimization
- **β‘ High-Performance Architecture**: Industry-leading high-performance WebSocket library
- **π― Event-Driven**: Comprehensive event system with middleware support
- **π‘οΈ Production Ready**: RFC 6455 compliant, compression support, robust error handling
- **π§ Developer Friendly**: Simple API, type safety, extensive testing (65.3% coverage)
- **π Cross Platform**: Support for Go 1.21+ on all major platforms including WASM
## π Performance Benchmarks
> **Test Environment**: Apple M1 Max (10-core), 32GB RAM, Go 1.24.5 darwin/arm64
### β‘ Zero-Copy Read/Write & Zero-GC Metrics
**π Performance Metrics**
| Metric | Value | Unit |
|--------|-------|------|
| **Throughput** | 82,650 | ops/sec (35.23 MB/s) |
| **Latency** | 13,887 | ns/op (13.9 ΞΌs) |
| **Memory** | 327 | B/op (16 allocs/op) |
| **Efficiency** | 99.2% | (vs baseline) |
### π Performance Comparison Matrix
| Metric | Standard | Zero-Copy Read/Write & Zero-GC | Improvement | Multiplier |
|--------|----------|-----------|-------------|------------|
| **Throughput** | 37,077 ops/sec | 82,650 ops/sec | **+123%** β‘ | **2.23Γ** |
| **Latency** | 32,381 ns | 13,887 ns | **-57%** β‘ | **2.33Γ** |
| **Memory** | 742 B/op | 327 B/op | **-56%** β‘ | **2.27Γ** |
| **Allocations** | 32 allocs/op | 16 allocs/op | **-50%** β‘ | **2.00Γ** |
### π High-Performance Mode vs Other WebSocket Libraries
#### π Technical Features Comparison
| Library | Zero-Copy Read/Write & Zero-GC | Buffer Pooling | Event-Driven |
|---------|-------------------------------|----------------|--------------|
| **BlazeWave** | β
Native Support | β
Smart Pooling | β
Comprehensive Events |
| nhooyr/websocket | β None | β None | β None |
| gorilla/websocket | β None | β None | β
Basic Events |
| fasthttp/websocket | β None | β None | β
Basic Events |
#### π― Core Advantages
**π BlazeWave Core Advantages**
| Feature | Description | Advantage |
|---------|-------------|-----------|
| β‘ **Zero-Copy Read/Write & Zero-GC** | Zero Memory Copy, Zero GC Pressure | Ultimate Performance |
| π **Smart Buffer Pooling** | Cross-connection Memory Reuse, Reduced GC | Memory Efficiency |
| π― **Comprehensive Event System** | Middleware & Custom Events, Beyond Basic | Developer Experience |
| π‘οΈ **Production Ready** | RFC 6455 Compliant, Robust Error Handling | Enterprise Stability |
| π§ **Developer Friendly** | Simple API, Type Safety, Extensive Testing | Quick Start |
## π¦ Installation
```bash
go get github.com/heyehang/blazewave
```
## β‘ Performance Advantages
### High-Performance Architecture
BlazeWave's high-performance design eliminates unnecessary memory allocations:
- **Direct Buffer Access**: High-frequency read/write operations bypass intermediate buffers
- **Memory Pool Reuse**: Smart buffer pooling reduces GC pressure
### Memory Efficiency
- **Buffer Pooling**: Reusable buffers across connections
- **Timer Pooling**: Reusable timers across connections
- **GC-Friendly**: Reduced garbage collection overhead
### High Throughput
- **High Performance Processing**: Optimized message handling pipeline
- **Sustained I/O**: Efficient network I/O processing
- **Concurrency Optimization**: Buffer pool and timer optimization
## π Quick Start
> **Note**: The following examples demonstrate standard mode usage. For zero-copy read/write & zero-GC mode advanced usage, please refer to [BlazeWave Pulse](https://github.com/heyehang/blazewave-pulse) or check local examples or unit tests.
> **π‘ Tip**: Zero-copy read/write & zero-GC mode is achieved by using optimized buffer pools and shared timer pools, suitable for production environments.
### Standard Mode
#### Server
```go
package main
import (
"context"
"log"
"net/http"
"github.com/heyehang/blazewave"
// "github.com/heyehang/blazewave/core/pool" // Required for zero-copy read/write & zero-GC mode
// "github.com/heyehang/blazewave/core/timer" // Required for shared timer pool
)
func main() {
// Standard mode: use default configuration
server := blazewave.NewServer()
// Zero-copy read/write & zero-GC mode: use custom buffer pools and shared timer pool
// rPool := pool.NewPool(64, 4*1024)
// wPool := pool.NewPool(64, 4*1024)
// sharedTimer := timer.NewTimer(100) // Shared timer pool with capacity 100
// server := blazewave.NewServer(
// blazewave.WithServerReaderPool(rPool),
// blazewave.WithServerWriterPool(wPool),
// blazewave.WithServerHeartbeatTimer(sharedTimer),
// )
server.OnTextMessage(func(ctx context.Context, conn *blazewave.Conn, payload []byte) error {
log.Printf("Received: %s", string(payload))
return conn.Write(ctx, blazewave.MessageText, payload)
})
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
conn, err := server.Accept(w, r)
if err != nil {
log.Printf("Accept failed: %v", err)
return
}
defer conn.Close(blazewave.StatusNormalClosure, "")
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
#### Client
```go
package main
import (
"context"
"log"
"time"
"github.com/heyehang/blazewave"
// "github.com/heyehang/blazewave/core/pool" // Required for zero-copy read/write & zero-GC mode
// "github.com/heyehang/blazewave/core/timer" // Required for shared timer pool
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Standard mode: use default configuration
client := blazewave.NewClient()
// Zero-copy read/write & zero-GC mode: use custom buffer pools and shared timer pool
// rPool := pool.NewPool(64, 4*1024)
// wPool := pool.NewPool(64, 4*1024)
// sharedTimer := timer.NewTimer(100) // Shared timer pool with capacity 100
// client := blazewave.NewClient(
// blazewave.WithClientReaderPool(rPool),
// blazewave.WithClientWriterPool(wPool),
// blazewave.WithClientHeartbeatTimer(sharedTimer),
// )
// Register event handlers
client.OnConnect(func(ctx context.Context, conn *blazewave.Conn) error {
log.Println("Connected to server!")
return nil
})
client.OnTextMessage(func(ctx context.Context, conn *blazewave.Conn, payload []byte) error {
log.Printf("Received message: %s", string(payload))
return nil
})
client.OnDisconnect(func(ctx context.Context, conn *blazewave.Conn) error {
log.Println("Disconnected from server")
return nil
})
// Connect to server
conn, _, err := client.Dial(ctx, "ws://localhost:8080/ws", nil)
if err != nil {
log.Fatal("Dial failed:", err)
}
defer conn.Close(blazewave.StatusNormalClosure, "")
// Send message
err = conn.Write(ctx, blazewave.MessageText, []byte("Hello, BlazeWave!"))
if err != nil {
log.Fatal("Write failed:", err)
}
// Keep connection alive for a while
time.Sleep(5 * time.Second)
}
```
## ποΈ Architecture
**ποΈ BlazeWave Architecture Design**
### Core Components
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β BlazeWave Core Architecture β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ β
β β Application β β Connection β β Processing β β
β β β’ Event System βββββΊβ β’ Zero-Copy I/O βββββΊβ β’ Frame Parsing β β
β β β’ Middleware β β β’ Buffer Pool β β β’ Mask Processingβ β
β β β’ Heartbeat β β β’ Compression β β β’ Validation β β
β βββββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ β
β β Infrastructure β β Optimization β β Network Layer β β
β β β’ Buffer Pool β β β’ Zero-Copy β β β’ TCP/WebSocket β β
β β β’ Memory Reuse β β β’ Zero-GC β β β’ TLS Support β β
β β β’ GC Optimized β β β’ Event-Driven β β β’ Hijacking β β
β βββββββββββββββββββ βββββββββββββββββββ ββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
**Performance Optimization Notes**:
- **Buffer Pool Reuse**: Shared buffers across connections, reducing high-frequency memory allocations and GC pressure
- **Timer Pool Reuse**: Use `WithServerHeartbeatTimer()` and `WithClientHeartbeatTimer()` to share timer pools
- **Zero-Copy Read/Write & Zero-GC Optimization**: Use optimized buffer pools and shared timer pools
## π― Best Practices & Reference Projects
### Core Philosophy: Event-Driven Real-Time Applications
BlazeWave is designed around the core philosophy of **event-driven real-time applications**. For the best implementation examples and production-ready patterns, check out our reference project:
#### **[BlazeWave Pulse](https://github.com/heyehang/blazewave-pulse)** β‘
> **The definitive reference implementation showcasing BlazeWave's core concepts**
- **Real-time Collaboration**: Multi-user interactive applications
- **Event-Driven Architecture**: Complete event system implementation
- **Production Patterns**: Scalable, maintainable code structure
- **Performance Optimization**: High-performance, buffer pooling
This project demonstrates the best practices for building high-performance, real-time applications with BlazeWave.
## π Available Demos
Check out our interactive examples:
- **[Chat Demo](./examples/chat/)**: Real-time chat application with user management
- 
- **[Shared Mascot](./examples/shared-mascot/)**: Collaborative mascot movement demo
- 
- **[More Examples](./examples/README.md)**: Complete list of available demos
## π Acknowledgments
- Inspired by and improved from [nhooyr/websocket](https://github.com/nhooyr/websocket)
- Built with Go's excellent standard library
## β Star History
[](https://star-history.com/#heyehang/blazewave&Date)
---
**BlazeWave** - Where Fire Meets Wave β‘
[](https://github.com/heyehang/blazewave)
[](https://github.com/heyehang/blazewave)