glupbit/websocket/connection

WebSocket connection management via stratus actors.

Handles public and private (authenticated) connections with automatic message routing based on type / method fields in incoming JSON. Supports automatic reconnection with exponential backoff.

Types

Configuration for automatic WebSocket reconnection.

pub type ReconnectConfig {
  ReconnectConfig(
    subscriptions: List(subscription.Subscription),
    format: subscription.WsFormat,
    initial_delay_ms: Int,
    max_delay_ms: Int,
    on_reconnect: fn() -> Nil,
  )
}

Constructors

User message type for the WebSocket actor.

pub type WsCommand {
  Subscribe(
    List(subscription.Subscription),
    subscription.WsFormat,
  )
  ListSubscriptions
}

Constructors

Handle returned from connecting to a WebSocket.

pub type WsHandle =
  process.Subject(stratus.InternalMessage(WsCommand))

Decoded WebSocket message.

pub type WsMessage {
  TickerMsg(subscription.TickerData)
  TradeMsg(subscription.TradeData)
  OrderbookMsg(subscription.OrderbookData)
  SubscriptionListMsg(subscription.ListSubscriptionsResponse)
  StatusMsg(String)
  ErrorMsg(name: String, message: String)
  RawMsg(String)
}

Constructors

Internal WebSocket state.

pub type WsState(user_state) {
  WsState(
    on_message: fn(user_state, WsMessage) -> user_state,
    user_state: user_state,
  )
}

Constructors

  • WsState(
      on_message: fn(user_state, WsMessage) -> user_state,
      user_state: user_state,
    )

Values

pub fn connect_private(
  credentials creds: auth.Credentials,
  state user_state: user_state,
  on_message handler: fn(user_state, WsMessage) -> user_state,
) -> Result(
  process.Subject(stratus.InternalMessage(WsCommand)),
  stratus.InitializationError,
)

Connect to the private WebSocket with authentication.

pub fn connect_public(
  state user_state: user_state,
  on_message handler: fn(user_state, WsMessage) -> user_state,
) -> Result(
  process.Subject(stratus.InternalMessage(WsCommand)),
  stratus.InitializationError,
)

Connect to the public WebSocket.

pub fn connect_public_with_reconnect(
  state user_state: user_state,
  on_message handler: fn(user_state, WsMessage) -> user_state,
  reconnect config: ReconnectConfig,
) -> Result(
  process.Subject(stratus.InternalMessage(WsCommand)),
  stratus.InitializationError,
)

Connect to the public WebSocket with automatic reconnection. On disconnect, retries with exponential backoff (1s, 2s, 4s, … up to max_delay_ms). Re-subscribes to the same feeds after successful reconnection.

pub fn decode_ws_message(text: String) -> WsMessage
pub fn list_subscriptions(
  conn: process.Subject(stratus.InternalMessage(WsCommand)),
) -> Nil

Query the list of active subscriptions on the WebSocket.

pub fn subscribe(
  conn: process.Subject(stratus.InternalMessage(WsCommand)),
  subscriptions: List(subscription.Subscription),
  format: subscription.WsFormat,
) -> Nil

Send a subscription request over the WebSocket.

Search Document