> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aptly.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Brook SDK Documentation

> Real-time fault-tolerant pub/sub SDK for JavaScript, Go, and React

## Welcome to Brook

Brook is a powerful real-time messaging SDK that provides fault-tolerant pub/sub functionality with automatic reconnection, message replay, and seamless React integration.

<CardGroup cols={2}>
  <Card title="Getting Started" icon="rocket" href="/getting-started">
    Get up and running with Brook in under 5 minutes
  </Card>

  <Card title="Live Demo" icon="play" href="https://demo.aptly.cloud">
    See Brook in action with our interactive demo
  </Card>

  <Card title="Get API Key" icon="key" href="https://console.aptly.cloud/brook">
    Sign up and get your API key from the console
  </Card>

  <Card title="GitHub" icon="github" href="https://github.com/aptly-cloud/brook">
    View the source code and contribute
  </Card>
</CardGroup>

## Key Features

<CardGroup cols={3}>
  <Card title="Real-time Messaging" icon="bolt">
    WebSocket-based pub/sub with instant message delivery
  </Card>

  <Card title="Fault Tolerant" icon="shield-check">
    Automatic reconnection with exponential backoff
  </Card>

  <Card title="Message Replay" icon="clock-rotate-left">
    Never miss a message with automatic replay
  </Card>

  <Card title="React Hooks" icon="react">
    Seamless integration with React applications
  </Card>

  <Card title="TypeScript Support" icon="code">
    Full TypeScript support with comprehensive types
  </Card>

  <Card title="Lightweight" icon="feather">
    Minimal bundle size for optimal performance
  </Card>
</CardGroup>

## Quick Example

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    import Brook from '@aptly-sdk/brook';

    const client = new Brook({ apiKey: 'your-api-key' });
    await client.connect();

    const channel = client.realtime.channel('my-topic');

    channel.stream((message, metadata) => {
      console.log('Received:', message);
    });

    await channel.publish({ text: 'Hello World!' });
    ```
  </Tab>

  <Tab title="React">
    ```jsx theme={null}
    import { useState } from 'react';
    import { BrookProvider, useStream, usePublish } from '@aptly-sdk/brook/react';

    function App() {
      return (
        <BrookProvider config={client}>
          <ChatComponent />
        </BrookProvider>
      );
    }

    function ChatComponent() {
      const [latestMessage, setLatestMessage] = useState(null);
      const publish = usePublish('chat-room');

      useStream('chat-room', (message, metadata) => {
        setLatestMessage(message);
      });

      return (
        <div>
          <p>Latest: {JSON.stringify(latestMessage)}</p>
          <button onClick={() => publish({ text: 'Hi!' })}>
            Send
          </button>
        </div>
      );
    }
    ```
  </Tab>

  <Tab title="Browser/CDN">
    ```html theme={null}
    <script type="module">
      import Brook from 'https://cdn.jsdelivr.net/npm/@aptly-sdk/brook@0.0.27/+esm';

      const client = new Brook({ apiKey: 'your-api-key' });
      await client.connect();

      const channel = client.realtime.channel('my-topic');

      channel.stream((message) => {
        console.log('Received:', message);
      });

      await channel.publish({ text: 'Hello from browser!' });
    </script>
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    package main

    import (
        "fmt"
        "log"
        aptly "github.com/aptly-cloud/go"
    )

    func main() {
        client, _ := aptly.NewClient(aptly.Config{
            APIKey: "your-api-key",
        })
        defer client.Cleanup()

        client.Connect()

        channel, _ := client.Realtime().Channel("my-topic")

        channel.Stream(func(data interface{}, metadata aptly.MessageMetadata) {
            fmt.Printf("Received: %v\n", data)
        })

        channel.Publish(map[string]string{
            "text": "Hello World!",
        })
    }
    ```
  </Tab>

  <Tab title="cURL">
    ```bash theme={null}
    curl -X POST https://connect.aptly.cloud/realtime \
      -H "Content-Type: application/json" \
      -H "x-api-key: your-api-key" \
      -H "Origin: https://your.allowed.website \
      -d '{
        "channel": "my-topic",
        "message": {"text": "Hello from cURL!"}
      }'
    ```
  </Tab>
</Tabs>

## SDK Guides

<CardGroup cols={2}>
  <Card title="JavaScript SDK" icon="js" href="/javascript-sdk">
    Complete guide for vanilla JavaScript applications
  </Card>

  <Card title="Go SDK" icon="golang" href="/go-sdk">
    Build real-time applications with Go
  </Card>

  <Card title="React SDK" icon="react" href="/react-sdk">
    Use Brook with React hooks and components
  </Card>

  <Card title="REST API" icon="code" href="/rest-api">
    Publish messages using HTTP/REST endpoints
  </Card>
</CardGroup>

## Why Brook?

<AccordionGroup>
  <Accordion title="Fault Tolerant by Design">
    Brook automatically handles connection failures with intelligent retry logic
    and exponential backoff. Your application stays resilient even in unstable
    network conditions.
  </Accordion>

  <Accordion title="Never Miss a Message">
    Message replay ensures that when your client reconnects, it automatically
    receives all messages that were sent while offline. No data loss,
    guaranteed.
  </Accordion>

  <Accordion title="Developer Friendly">
    Simple, intuitive API that works seamlessly with vanilla JavaScript or
    React. Get started in minutes with comprehensive documentation and examples.
  </Accordion>

  <Accordion title="Production Ready">
    Built for scale with lightweight footprint, TypeScript support, and
    battle-tested reliability. Used by teams building real-time applications.
  </Accordion>
</AccordionGroup>

## Use Cases

Brook is perfect for building:

* Real-time chat applications
* Live notifications and alerts
* Collaborative editing tools
* Live dashboards and analytics
* Multiplayer game updates
* IoT device communication
* Live sports scores and updates

## Get Started

<Steps>
  <Step title="Install the SDK">`bash npm install @aptly-sdk/brook `</Step>

  <Step title="Get Your API Key">
    Sign up at [console.aptly.cloud/brook](https://console.aptly.cloud/brook)
  </Step>

  <Step title="Start Building">
    Follow our [Getting Started](/getting-started) guide to build your first
    real-time app
  </Step>
</Steps>

## Community & Support

<CardGroup cols={2}>
  <Card title="GitHub Issues" icon="github" href="https://github.com/aptly-cloud/brook">
    Report bugs and request features
  </Card>

  <Card title="Documentation" icon="book-open" href="/getting-started">
    Read the full documentation
  </Card>
</CardGroup>
