# `ZenWebsocket.Examples.UsagePatterns`
[🔗](https://github.com/ZenHive/zen_websocket/blob/v0.8.0/lib/zen_websocket/examples/usage_patterns.ex#L1)

Examples of different WebSocket client usage patterns.
This module demonstrates three ways to use ZenWebsocket:
1. Direct connection (no supervision)
2. Using ClientSupervisor
3. Direct supervision in your app

# `client_supervisor_example`

```elixir
@spec client_supervisor_example() :: :ok
```

Pattern 2: Using ClientSupervisor for automatic restarts.

Best for:
- Production systems with multiple connections
- Dynamic connection management
- When you need a connection pool

Note: You must add ClientSupervisor to your supervision tree first!

## Returns
`:ok` after demonstrating the supervisor pattern.

# `direct_connection_example`

```elixir
@spec direct_connection_example() :: :ok
```

Pattern 1: Direct connection without supervision.

Best for:
- Development and testing
- Short-lived connections
- Scripts and one-off tasks

## Returns
`:ok` after demonstrating the connection pattern.

# `supervised_client_spec`

```elixir
@spec supervised_client_spec() :: {module(), keyword()}
```

Pattern 3: Direct supervision in your application.

Best for:
- Fixed set of connections
- When each connection has a specific role
- Simple production deployments

Add this to your application supervisor:

    children = [
      {ZenWebsocket.Client, [
        url: "wss://test.deribit.com/ws/api/v2",
        id: :deribit_client,
        handler: fn msg -> send(MyApp.Consumer, {:ws, msg}) end,
        heartbeat_config: %{type: :deribit, interval: 30_000}
      ]}
    ]

## Returns
A child specification tuple for use with Supervisor.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
