Installation
Initialization
Initialize the Brook client with your API key:Configuration Options
Recommended: Use Server Keys Go applications typically run in backend
environments. We recommend using Server Keys for Go applications. Always store
your server key in environment variables and never commit it to version
control.
Connecting to the Server
Establish a connection to the Brook server:Subscribing to Messages
Subscribe to a channel/topic to receive real-time messages:Message Metadata
Every message comes with metadata that provides context:Type-Safe Message Handling
You can type-assert message data to your specific types:Publishing Messages
Using the SDK
Publish messages through the WebSocket connection:Using HTTP (REST API)
Publish messages via HTTP when WebSocket is not available:Using Standard HTTP Client
You can also use Go’s standard HTTP client:Observing Connection Status
Monitor connection status changes to handle reconnections and errors:Connection States
Complete Example
View Complete Example
See a full working example with all features on GitHub
Advanced Features
Getting Client Information
Channel Statistics
Working with Multiple Channels
Graceful Shutdown
Using Context for Cancellation
Error Handling
Best Practices
Message replay automatically handles missed messages when you reconnect,
ensuring no data loss. Check
metadata.Replay to distinguish between new and
replayed messages.Additional Recommendations
- Use defer for cleanup: Always use
deferto ensure cleanup functions are called - Handle errors: Check errors from all SDK methods
- Type assertions: Safely type-assert message data before using it
- Goroutine safety: All SDK operations are goroutine-safe, but handle your own data structures carefully
- Context integration: Use Go contexts for better cancellation control
Thread Safety
The Go SDK is designed to be thread-safe and can be safely used across multiple goroutines:Testing
Here’s how to write tests for applications using the Brook SDK:API Reference
Client Methods
NewClient(config Config) (*Client, error)- Create new clientConnect() error- Connect to serverDisconnect()- Disconnect from serverCleanup()- Clean up resourcesRealtime() *Realtime- Get realtime interfaceChannel(name string) (*Channel, error)- Create/get channelIsConnected() bool- Check connection statusIsAuthenticated() bool- Check authentication statusGetClientID() string- Get client IDGetConnectionStatus() ConnectionState- Get connection stateGetActiveChannels() []string- Get active channel namesGetStats() Stats- Get statisticsPublishHTTP(channel string, message interface{}) (map[string]interface{}, error)- Publish via HTTPOnConnectivityChange(callback ConnectivityCallback) func()- Subscribe to connectivity changes
Channel Methods
Stream(callback MessageHandler) (func(), error)- Subscribe to messagesUnstream(callback MessageHandler)- Unsubscribe callbackPublish(message interface{}) error- Publish message via WebSocketSend(message interface{}) error- Alias for PublishClose()- Close channelGetStats() ChannelStats- Get channel statisticsResubscribe()- Manually resubscribe (usually automatic)
Types
Next Steps
Complete Example
View a full working example on GitHub
JavaScript SDK
Learn how to use Brook with JavaScript
React SDK
Learn how to use Brook with React hooks
REST API
Publish messages using REST API
Authentication
Learn about API keys and authentication