Skip to main content

Installation

Initialization

Initialize the Brook client with your API key:

Configuration Options

Key Type Selection

Learn More About Key Types

Read the complete guide on Public Keys vs Server Keys

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:

Publishing Messages

Using the SDK

Publish messages through the WebSocket connection:

Using Fetch (REST API)

Publish messages via HTTP when WebSocket is not available:

Using Axios

You can also use axios or any HTTP client:

Observing Connection Status

Monitor connection status changes to handle reconnections and errors:

Connection States

Complete Example

Here’s a complete example showing all features together:

Advanced Features

Getting Client Information

Channel Statistics

Cleanup

Always cleanup when you’re done to prevent memory leaks:

Error Handling

Best Practices

Always handle connection state changes to provide feedback to users when connection is lost or restored.
Don’t forget to cleanup by calling unsubscribe() or client.disconnect() when done to prevent memory leaks.
Message replay automatically handles missed messages when you reconnect, ensuring no data loss.

Using Brook with CDN (ESM)

For quick prototyping or simple HTML pages, you can use Brook directly from a CDN without a build step:
The CDN approach is perfect for demos, quick prototypes, and learning. For production applications, use the npm package with a build tool.

Next Steps

React SDK

Learn how to use Brook with React hooks

REST API

Publish messages using REST API