A high-performance WebSocket server built in Go for real-time blockchain data streaming, specifically designed for gas price monitoring, vault state updates, and home dashboard data.
- Real-time WebSocket connections for live data streaming
- Gas price monitoring with configurable time windows and TWAP calculations
- Vault state management with user-specific subscriptions
- Home dashboard data streaming
- Concurrent subscriber management with thread-safe operations
- PostgreSQL integration for data persistence
- Health check endpoints for monitoring
- Comprehensive test coverage for all API components
# Build and run
make build && make runThe server follows a clean, modular architecture:
server/
βββ api/
β βββ general/ # Gas price and general data endpoints
β βββ home/ # Home dashboard data endpoints
β βββ vault/ # Vault state and user data endpoints
β βββ utils/ # Shared utilities
βββ types/ # Type definitions and interfaces
βββ db/ # Database layer and repositories
βββ models/ # Data models and structures
βββ validations.go # Request validation logic
/health- Health check endpoint/subscribeGas- WebSocket endpoint for gas price data
/subscribeHome- WebSocket endpoint for home dashboard data
/subscribeVault- WebSocket endpoint for vault state updates
Subscribe to real-time gas price data with configurable parameters:
{
"startTimestamp": 1000,
"endTimestamp": 2000,
"roundDuration": 960
}Round Duration Options:
960- 12-minute TWAP13200- 3-hour TWAP2631600- 30-day TWAP
Subscribe to vault-specific updates:
{
"address": "0x...",
"vaultAddress": "0x...",
"userType": "user"
}- Go 1.22.5+
- PostgreSQL database
- Docker (optional, for containerized development)
-
Clone the repository
git clone <repository-url> cd pitchlake-websocket
-
Install dependencies
go mod tidy
-
Set up environment variables
# Copy and configure environment file cp .env.example .env -
Run the server
go run .
# Build and run with Docker Compose
docker-compose up --build
# Or build manually
docker build -t pitchlake-websocket .
docker run -p 8080:8080 pitchlake-websocketThe project includes comprehensive test coverage with both unit and integration tests. All testing commands are available via Makefile for easy development.
# Using Makefile (recommended)
make test
# Raw Go command
go test ./...# Using Makefile (recommended)
make test-unit
# Raw Go command
go test ./server/api/... ./server/validations/...# Using Makefile (recommended)
make test-integration
# Raw Go command
go test ./server/...# Vault API tests
go test ./server/api/vault/...# Using Makefile (recommended)
make test-coverage
# Raw Go command
go test -cover ./...
# Coverage by specific package
go test -cover ./server/validations/...Unit Tests (Fast):
βββ server/api/general/ # Handler tests (6 test cases)
βββ server/api/home/ # Handler tests (6 test cases)
βββ server/api/vault/ # Handler tests (6 test cases)
βββ server/validations/ # Validation tests (22 test cases)
Total: 40 unit tests
Integration Tests (Slower):
βββ server/integration_test.go # WebSocket tests (4 test cases)
SubscriberGas- Gas price subscription dataSubscriberVault- Vault subscription dataSubscriberHome- Home dashboard subscription dataBlockResponse- Blockchain block data with TWAP values
Block- Blockchain block informationVaultState- Current vault stateLiquidityProviderState- Liquidity provider statusOptionBuyer- Option buyer informationOptionRound- Option round details
The server uses mutex-protected subscriber management to ensure thread-safe operations:
SubscribersWithLock- Thread-safe subscriber collections- Concurrent subscriber addition/removal - Safe for high-traffic scenarios
- Message buffering - Configurable buffer sizes for performance
- Message buffering with configurable buffer sizes
- Efficient WebSocket handling using the
coder/websocketlibrary - Database connection pooling with
pgx - Graceful connection handling with timeout management
- Connection timeout management
- Graceful error recovery
- Comprehensive logging for debugging
Key configuration options:
type GeneralRouter struct {
subscriberMessageBuffer int // Message buffer size
Subscribers SubscribersWithLock
log log.Logger
pool pgxpool.Pool
}