ZenWebsocket (ZenWebsocket v0.4.2)

Copy Markdown View Source

A robust WebSocket client library for Elixir, built on Gun transport.

Designed for financial APIs (cryptocurrency exchanges like Deribit) but works with any WebSocket endpoint. Provides automatic reconnection, heartbeat management, rate limiting, and request/response correlation.

Quick Start

# Connect to a WebSocket endpoint
{:ok, client} = ZenWebsocket.Client.connect("wss://test.deribit.com/ws/api/v2")

# Send a message
:ok = ZenWebsocket.Client.send_message(client, Jason.encode!(%{method: "public/test"}))

# Subscribe to channels
:ok = ZenWebsocket.Client.subscribe(client, ["trades.BTC-PERPETUAL.raw"])

# Check connection state
:connected = ZenWebsocket.Client.get_state(client)

# Close when done
:ok = ZenWebsocket.Client.close(client)

Supervised Connections

For production use, ZenWebsocket.ClientSupervisor manages connection pools with health-based load balancing:

# Start the supervisor (add to your application supervision tree)
ZenWebsocket.ClientSupervisor.start_link([])

# Start managed connections
{:ok, client} = ZenWebsocket.ClientSupervisor.start_client("wss://example.com/ws")

# Route to healthiest connection
:ok = ZenWebsocket.ClientSupervisor.send_balanced(message)

Key Modules

Client API

Infrastructure

Observability

Protocol

Platform Examples

See ZenWebsocket.Examples.DeribitAdapter for a production-ready adapter demonstrating authentication, subscription management, and heartbeat handling.

Self-Describing API

All public modules are annotated with descripex for progressive discovery:

ZenWebsocket.describe()                        # Library overview
ZenWebsocket.describe(:client)                 # Client functions
ZenWebsocket.describe(:client, :connect)       # Full connect details

Summary

Functions

Return the list of modules registered with this library.

Return a Level 1 overview of all modules in this library.

Return Level 2 function list for a module (by full atom or short name).

Return Level 3 function detail (or nil if not found).

Functions

__descripex_modules__()

@spec __descripex_modules__() :: [module()]

Return the list of modules registered with this library.

describe()

@spec describe() :: [map()]

Return a Level 1 overview of all modules in this library.

describe(mod_or_short)

@spec describe(module() | atom()) :: [map()]

Return Level 2 function list for a module (by full atom or short name).

describe(mod_or_short, func_name)

@spec describe(module() | atom(), atom()) :: map() | nil

Return Level 3 function detail (or nil if not found).