WebsockexAdapter.Examples.UsagePatterns (WebsockexAdapter v0.1.1)
View SourceExamples of different WebSocket client usage patterns. This module demonstrates three ways to use WebsockexAdapter:
- Direct connection (no supervision)
- Using ClientSupervisor
- Direct supervision in your app
Summary
Functions
Pattern 2: Using ClientSupervisor for automatic restarts.
Pattern 1: Direct connection without supervision.
Pattern 3: Direct supervision in your application.
Functions
@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.
@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.
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 = [
{WebsockexAdapter.Client, [
url: "wss://test.deribit.com/ws/api/v2",
id: :deribit_client,
heartbeat_config: %{type: :deribit, interval: 30_000}
]}
]
Returns
A child specification tuple for use with Supervisor.