Skip to main content
The WebSocket API provides real-time streaming of market data, including price updates, trade executions, and orderbook depth. Connect once and receive continuous updates for the markets you’re interested in.
The websocket API requires a valid API key. You can get one by contacting us.
The WebSocket API is ideal for building real-time trading interfaces, price tickers, and trade feeds. For historical data or one-time queries, use the REST API endpoints.

Connection

Connect to the WebSocket endpoint:
wss://prediction-markets-api.dflow.net/api/v1/ws

Channels

The WebSocket API supports three channels:
ChannelDescription
pricesReal-time bid/ask price updates for markets
tradesReal-time trade execution updates
orderbookReal-time orderbook depth updates for markets

Subscription Management

After connecting, send JSON messages to subscribe or unsubscribe from channels. Each channel maintains independent subscription state.

Subscribe to All Markets

Subscribe to receive updates for all markets on a channel:
{
  "type": "subscribe",
  "channel": "prices",
  "all": true
}

Subscribe to Specific Tickers

Subscribe to receive updates for specific market tickers:
{
  "type": "subscribe",
  "channel": "prices",
  "tickers": ["BTCD-25DEC0313-T92749.99", "SPX-25DEC0313-T5000"]
}

Unsubscribe from All Markets

Unsubscribe from all markets on a channel:
{
  "type": "unsubscribe",
  "channel": "prices",
  "all": true
}

Unsubscribe from Specific Tickers

Unsubscribe from specific market tickers:
{
  "type": "unsubscribe",
  "channel": "prices",
  "tickers": ["BTCD-25DEC0313-T92749.99"]
}
Unsubscribing from specific tickers has no effect if you are subscribed to “all” for that channel. You must first unsubscribe from “all” before managing individual ticker subscriptions.

Subscription Behavior

  • Subscribing to "all": true clears any specific ticker subscriptions for that channel.
  • Subscribing to specific tickers disables “all” mode for that channel.
  • Each channel (prices, trades, and orderbook) maintains independent subscription state.
  • You can be subscribed to all prices while only subscribing to specific tickers for trades or orderbooks, or any combination.

Quick Start Example

const WEBSOCKET_URL = "wss://prediction-markets-api.dflow.net/api/v1/ws";

const ws = new WebSocket(WEBSOCKET_URL);

ws.onopen = () => {
  console.log("Connected to WebSocket");

  // Subscribe to all price updates
  ws.send(
    JSON.stringify({
      type: "subscribe",
      channel: "prices",
      all: true,
    })
  );

  // Subscribe to all trade updates
  ws.send(
    JSON.stringify({
      type: "subscribe",
      channel: "trades",
      all: true,
    })
  );

  // Subscribe to all orderbook updates
  ws.send(
    JSON.stringify({
      type: "subscribe",
      channel: "orderbook",
      all: true,
    })
  );
};

ws.onmessage = (event) => {
  const message = JSON.parse(event.data);

  switch (message.channel) {
    case "prices":
      console.log("Price update:", message);
      break;
    case "trades":
      console.log("Trade update:", message);
      break;
    case "orderbook":
      console.log("Orderbook update:", message);
      break;
  }
};

ws.onerror = (error) => {
  console.error("WebSocket error:", error);
};

ws.onclose = () => {
  console.log("WebSocket connection closed");
};

Best Practices

Connection Management

Implement reconnection logic with exponential backoff to handle disconnections gracefully.

Subscription Scope

Subscribe only to the markets you need. Use specific tickers rather than “all” when possible to reduce bandwidth.

Message Handling

Process messages asynchronously to avoid blocking the WebSocket connection during high-volume periods.

Error Handling

Always implement onerror and onclose handlers to detect and respond to connection issues.

Next Steps