WebsockexAdapter.Examples.UsagePatterns (WebsockexAdapter v0.1.1)

View Source

Examples of different WebSocket client usage patterns. This module demonstrates three ways to use WebsockexAdapter:

  1. Direct connection (no supervision)
  2. Using ClientSupervisor
  3. 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

client_supervisor_example()

@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()

@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()

@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 = [
  {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.