ZenWebsocket Examples

Copy Markdown View Source

Practical examples of using ZenWebsocket, all written and tested in-tree under lib/zen_websocket/examples/. Examples and their test suites live in the library repository; a separate examples/<name>/ mix project structure was evaluated and reverted in favor of in-tree examples to ensure they stay synchronized with the library. This page is a navigable index; each entry points at the module source and test, which are the single source of truth rather than pasting code blocks that can drift.

Module Index

lib/zen_websocket/examples/

ModuleFileDemonstratesTests
ZenWebsocket.Examples.AdapterSupervisoradapter_supervisor.exSupervising adapter GenServers alongside ClientSupervisornone
ZenWebsocket.Examples.BatchSubscriptionManagerbatch_subscription_manager.exBatching subscription requests with configurable batch size/delaybatch_subscription_manager_test.exs
ZenWebsocket.Examples.DeribitAdapterderibit_adapter.exSimplified Deribit adapter built on DeribitRpcderibit_adapter_auth_test.exs, deribit_adapter_nil_client_test.exs
ZenWebsocket.Examples.DeribitGenServerAdapterderibit_genserver_adapter.exProduction-ready supervised Deribit adapter: monitored client, auto-reconnect, auth/subscription restorederibit_genserver_adapter_test.exs
ZenWebsocket.Examples.DeribitRpcderibit_rpc.exShared Deribit JSON-RPC method/request builders used by both Deribit adaptersnone dedicated — exercised indirectly through the DeribitAdapter/DeribitGenServerAdapter test suites
ZenWebsocket.Examples.JsonRpcTransportjson_rpc_transport.exShared JSON-RPC send helper (Client.send_message/2 result normalization) used by both Deribit adaptersnone dedicated — exercised indirectly via deribit_adapter_nil_client_test.exs
ZenWebsocket.Examples.PlatformAdapterTemplateplatform_adapter_template.exMinimal template (connect/2, authenticate/2, extension points) for a new platform adapterplatform_adapter_template_test.exs
ZenWebsocket.Examples.SupervisedClientsupervised_client.exMinimal ClientSupervisor-based connection supervisionsupervised_client_test.exs
ZenWebsocket.Examples.UsagePatternsusage_patterns.exThree usage patterns: direct connection, ClientSupervisor, direct supervision in your own treenone

ClientSupervisor itself (not an example module, but exercised by this directory's supervision examples) has end-to-end coverage in supervised_connection_test.exs.

lib/zen_websocket/examples/docs/

A second set of examples, written to accompany this guide's code snippets:

ModuleFileDemonstratesTests
ZenWebsocket.Examples.Docs.BasicUsagebasic_usage.exBasic connect + request/response against Deribit testnetbasic_usage_test.exs
ZenWebsocket.Examples.Docs.ErrorHandlingerror_handling.exA GenServer wrapping a connection with retry-on-failureerror_handling_test.exs
ZenWebsocket.Examples.Docs.JsonRpcClientjson_rpc_client.exDeclaring JSON-RPC methods with ZenWebsocket.JsonRpc's defrpc macronone
ZenWebsocket.Examples.Docs.SubscriptionManagementsubscription_management.exMulti-channel subscription patternssubscription_management_test.exs

Running the Examples

# Run a single example's test
mix test test/zen_websocket/examples/basic_usage_test.exs

# Run every example test
mix test test/zen_websocket/examples/

# Run with coverage
mix test --cover test/zen_websocket/examples/

# Or explore interactively
iex -S mix
# In IEx, try the basic usage example
alias ZenWebsocket.Examples.Docs.BasicUsage

{:ok, client} = BasicUsage.deribit_testnet_example()

Deribit Integration

ZenWebsocket.Examples.DeribitAdapter and ZenWebsocket.Examples.DeribitGenServerAdapter are the in-tree Deribit integration examples — authentication, subscription management, and (for the GenServer adapter) monitored auto-reconnect with auth/subscription restore. Both share request-building through DeribitRpc and transport through JsonRpcTransport. Study deribit_genserver_adapter.ex for the production-supervised pattern. Its test suite (deribit_genserver_adapter_test.exs) runs against real Deribit testnet endpoints — all integration tests in this library use live API endpoints rather than mocks to ensure correctness against real provider behavior.

Extending for Your Platform

To create an adapter for your own platform:

  1. Study platform_adapter_template.ex
  2. Follow the adapter building guide
  3. Implement platform-specific authentication, message formatting, subscription management, and error handling
  4. Add tests against real API endpoints — see the Deribit adapters' test suites for the pattern
  5. Document platform-specific features

Additional Resources