Skip to main content

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

Always handle connection state changes to provide feedback when connection is lost or restored. Use OnConnectivityChange() to monitor connection health.
Don’t forget to cleanup by calling defer client.Cleanup() or defer unsubscribe() to prevent memory leaks and ensure proper resource cleanup.
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

  1. Use defer for cleanup: Always use defer to ensure cleanup functions are called
  2. Handle errors: Check errors from all SDK methods
  3. Type assertions: Safely type-assert message data before using it
  4. Goroutine safety: All SDK operations are goroutine-safe, but handle your own data structures carefully
  5. 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 client
  • Connect() error - Connect to server
  • Disconnect() - Disconnect from server
  • Cleanup() - Clean up resources
  • Realtime() *Realtime - Get realtime interface
  • Channel(name string) (*Channel, error) - Create/get channel
  • IsConnected() bool - Check connection status
  • IsAuthenticated() bool - Check authentication status
  • GetClientID() string - Get client ID
  • GetConnectionStatus() ConnectionState - Get connection state
  • GetActiveChannels() []string - Get active channel names
  • GetStats() Stats - Get statistics
  • PublishHTTP(channel string, message interface{}) (map[string]interface{}, error) - Publish via HTTP
  • OnConnectivityChange(callback ConnectivityCallback) func() - Subscribe to connectivity changes

Channel Methods

  • Stream(callback MessageHandler) (func(), error) - Subscribe to messages
  • Unstream(callback MessageHandler) - Unsubscribe callback
  • Publish(message interface{}) error - Publish message via WebSocket
  • Send(message interface{}) error - Alias for Publish
  • Close() - Close channel
  • GetStats() ChannelStats - Get channel statistics
  • Resubscribe() - 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