# Magic Socket

**Real-time channels, ready in your Magic app.**

Real-time broadcasting for Magic apps through the `Echo` facade, with a Reverb driver and a null driver. Built for a Reverb or other Pusher-protocol server; the API will look familiar to Laravel Echo users.

## One facade. Every channel.

Call `Echo` to listen on public, private and presence channels. The driver you select in `broadcasting.default` handles the connection.

### Three channel types

`Echo.channel(name)` for public, `Echo.private(name)` for private (auth through `/broadcasting/auth`) and `Echo.join(name)` for presence, with `members`, `onJoin`, `onLeave`.

[Learn more about Three channel types](https://anilcancakir.com/magic/docs/socket/basics/channels.md)

### Reconnects on its own

The Reverb driver reconnects with exponential backoff and jitter and resubscribes every channel after each handshake, so a dropped connection heals without app code.

[Learn more about Reconnects on its own](https://anilcancakir.com/magic/docs/socket/guides/connection.md)

### Pick a driver

`broadcasting.default` selects `reverb` for a Reverb or Pusher-protocol server. The default `null` driver does nothing, so an app without a socket server runs unchanged.

[Learn more about Pick a driver](https://anilcancakir.com/magic/docs/socket/basics/configuration.md)

### Skip the sender

While a socket is connected and `NetworkServiceProvider` is registered, an HTTP interceptor adds `X-Socket-ID` to each request, so the server's `toOthers()` skips the sender. `Echo.socketId` reads it.

[Learn more about Skip the sender](https://anilcancakir.com/magic/docs/socket/basics/configuration.md)

### Listen in controllers

The `ListensToBroadcasts` mixin lets a `MagicController` listen on a private channel and clean up with the controller (`BroadcastListeners.channel`).

[Learn more about Listen in controllers](https://anilcancakir.com/magic/docs/socket/basics/listeners.md)

### Test without a socket

`Echo.fake()` swaps in `FakeBroadcastManager` so a test asserts subscriptions and listeners without a socket, and `Echo.unfake()` restores it.

[Learn more about Test without a socket](https://anilcancakir.com/magic/docs/socket/guides/testing.md)

## Add it in seconds

```bash
flutter pub add magic_socket
```

Or add to `pubspec.yaml`:

```yaml
dependencies:
  magic_socket: ^1.0.1
```

Or give your AI agent this prompt:

```text
Read https://anilcancakir.com/magic/docs/socket/llms.txt and install the Magic Socket package in this project.
```

## Docs

### Getting Started

- [Installation](https://anilcancakir.com/magic/docs/socket/getting-started/installation.md)

### The Basics

- [Configuration](https://anilcancakir.com/magic/docs/socket/basics/configuration.md)
- [Channels](https://anilcancakir.com/magic/docs/socket/basics/channels.md)
- [Listeners](https://anilcancakir.com/magic/docs/socket/basics/listeners.md)

### Guides

- [Connection](https://anilcancakir.com/magic/docs/socket/guides/connection.md)
- [Extending](https://anilcancakir.com/magic/docs/socket/guides/extending.md)
- [Testing](https://anilcancakir.com/magic/docs/socket/guides/testing.md)
