Polymarket.MarketStream (Polymarket v0.2.0)

Copy Markdown View Source

CLOB market-data websocket client — the order-book stream.

Connects to wss://ws-subscriptions-clob.polymarket.com/ws/market, subscribes to a set of asset_ids (outcome token ids), and forwards book updates to a callback PID:

  • {:polymarket_market, :book, payload} — a full book snapshot (%{"asset_id", "bids", "asks", ...}), sent on subscribe and periodically;
  • {:polymarket_market, :price_change, payload} — incremental level changes (%{"asset_id", "changes": [%{"price","side","size"}]}).

This is the push-based alternative to polling GET /book — one persistent connection feeds book updates for all subscribed tokens, so the snapshot layer never has to poll (which can be rate-limiting). Book maintenance (applying incremental :price_change events to a stored book snapshot) is the consumer's job.

Mirrors Polymarket.RTDS (WebSockex, PID callback, text PING) plus a reconnect backoff so transient drops / 429s don't storm.

Options

  • :callback (required) — PID to receive events.
  • :assets (required, non-empty) — token ids to subscribe.
  • :url — override (default the CLOB market channel).
  • :ping_interval — ms (default 10_000).
  • :name — optional process name.

Summary

Functions

Decode a raw CLOB market frame into a list of {event_type, payload} tuples. Pure — the frame can be a single event object or a JSON array of them. Unknown/PONG/non-book frames yield [].

Functions

decode_events(text)

@spec decode_events(String.t()) :: [{atom(), map()}]

Decode a raw CLOB market frame into a list of {event_type, payload} tuples. Pure — the frame can be a single event object or a JSON array of them. Unknown/PONG/non-book frames yield [].

default_url()

@spec default_url() :: String.t()

start_link(opts)

@spec start_link(keyword()) :: {:ok, pid()} | {:error, term()}