View Source Hyperliquid.Transport.WebSocket (hyperliquid v0.2.2)

WebSocket transport using Mint for Hyperliquid API.

Features:

  • Mint-based WebSocket client
  • Automatic reconnection with exponential backoff
  • Proxy support (HTTP CONNECT tunneling)
  • Subscription management with callbacks
  • Automatic ping/pong handling
  • Message buffering during reconnection

Usage

# Start without proxy
{:ok, pid} = WebSocket.start_link(url: "wss://api.hyperliquid.xyz/ws")

# Start with proxy
{:ok, pid} = WebSocket.start_link(
  url: "wss://api.hyperliquid.xyz/ws",
  proxy: %{host: "proxy.example.com", port: 8080},
  proxy_auth: {"username", "password"}
)

# Subscribe to a channel
WebSocket.subscribe(pid, %{
  type: "webData2",
  user: "0x..."
}, fn event ->
  IO.inspect(event, label: "WebData2 Event")
end)

# Unsubscribe
WebSocket.unsubscribe(pid, subscription_id)

Summary

Functions

Returns a specification to start this module under a supervisor.

Check if the WebSocket is currently connected.

Start the WebSocket client.

Get current connection statistics.

Subscribe to a channel with a callback.

Unsubscribe from a channel using the subscription ID.

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Check if the WebSocket is currently connected.

Start the WebSocket client.

Options

  • :url - WebSocket URL (required)
  • :proxy - Proxy configuration map with :host and :port
  • :proxy_auth - Tuple of {username, password} for proxy authentication
  • :name - Process name (optional)

Examples

{:ok, pid} = WebSocket.start_link(url: "wss://api.hyperliquid.xyz/ws")

{:ok, pid} = WebSocket.start_link(
  url: "wss://api.hyperliquid.xyz/ws",
  proxy: %{host: "10.0.0.1", port: 8080},
  proxy_auth: {"user", "pass"}
)

Get current connection statistics.

Link to this function

subscribe(pid, channel, callback)

View Source

Subscribe to a channel with a callback.

Returns {:ok, subscription_id} which can be used to unsubscribe later.

Examples

{:ok, sub_id} = WebSocket.subscribe(pid, %{
  type: "webData2",
  user: "0x1234..."
}, fn event ->
  IO.inspect(event)
end)
Link to this function

unsubscribe(pid, subscription_id)

View Source

Unsubscribe from a channel using the subscription ID.