RealMarketAPI
Docs/WebSocket

WebSocket Streaming

Real-time candle push stream · JavaScript, Python, C#, Java, Go examples

Why WebSocket?

The WebSocket endpoint opens a persistent, bidirectional TCP connection. The server pushes a new OHLCV frame the instant each candle period closes — no polling, no extra HTTP overhead, no wasted quota.

⚡ Zero polling overhead

Candles are pushed to you the moment they close. No repeated HTTP calls.

💰 Doesn't count against quota

The persistent connection is free to hold open; only the initial handshake counts.

📉 Lower latency

Avoid TCP setup, TLS negotiation, and HTTP framing on each update.

🔗 Same response shape

Identical OHLCV JSON to REST — no extra parsing needed.

WSS

/price

Opens a persistent WebSocket connection. The server pushes a new OHLCV frame after each completed candle. Requires a Pro or Business plan.

Connection Parameters
NameTypeDescription
apiKey*stringYour API key.
symbolCode*stringSymbol to stream (e.g. XAUUSD, BTCUSD).
timeFrame*stringCandle interval: M1 · M5 · M15 · H1 · H4 · D1.

Connection URL

WebSocket URL
CONNECTwss://api.realmarketapi.com/price?apiKey=YOUR_API_KEY&symbolCode=XAUUSD&timeFrame=M1

Incoming frame (JSON)

candle-frame.json
{
  "SymbolCode": "XAUUSD",
  "OpenPrice":  5168.43,
  "ClosePrice": 5174.00,
  "HighPrice":  5176.85,
  "LowPrice":   5165.20,
  "Bid":        5173.75,
  "Ask":        5174.25,
  "Volume":     1249.33,
  "OpenTime":   "2026-03-11T09:20:00Z"
}
▶️

Stream live candles in the Playground

Connect a real WebSocket stream and watch candle frames arrive live — directly in your browser.

Open Playground

Code Examples

Basic connection examples for getting started. These connect, listen for candle frames, and log them to the console. See the auto-reconnect pattern below for production use.

ws-basic.js
// Node.js / Browser — basic WebSocket connection
const ws = new WebSocket(
  "wss://api.realmarketapi.com/price?apiKey=YOUR_API_KEY&symbolCode=XAUUSD&timeFrame=M1"
)

ws.onopen    = () => console.log("[RealMarketAPI] Connected")
ws.onmessage = ({ data }) => {
  const candle = JSON.parse(data)
  console.log(candle.SymbolCode, candle.ClosePrice, candle.Bid, candle.Ask)
}
ws.onerror   = (err) => console.error("[RealMarketAPI] Error", err)
ws.onclose   = ()    => console.log("[RealMarketAPI] Disconnected")

Auto-Reconnect Pattern

Network interruptions happen. A production app must implement automatic reconnection with exponential back-off — otherwise a single dropped connection silently stops your live feed forever.

📐 Back-off strategy

  • Start at 1 second on first disconnect.
  • Double the delay on each consecutive failure.
  • Cap at 30 seconds to bound maximum wait.
  • Reset to 1 s on every successful onopen event.
ws-reconnect.js
// JavaScript — auto-reconnect with exponential back-off (production)
const API_KEY   = "YOUR_API_KEY"
const SYMBOL    = "XAUUSD"
const TIMEFRAME = "M1"
const WS_URL    = `wss://api.realmarketapi.com/price?apiKey=${API_KEY}&symbolCode=${SYMBOL}&timeFrame=${TIMEFRAME}`

let ws
let delay = 1_000  // start at 1 s

function connect() {
  ws = new WebSocket(WS_URL)

  ws.onopen = () => {
    console.log("Connected")
    delay = 1_000   // reset on successful connection
  }

  ws.onmessage = ({ data }) => {
    const candle = JSON.parse(data)
    console.log(candle.SymbolCode, candle.ClosePrice)
    // ── dispatch to your state / chart ─────────────────────────
  }

  ws.onclose = () => {
    console.warn(`Disconnected. Reconnecting in ${delay}ms…`)
    setTimeout(connect, delay)
    delay = Math.min(delay * 2, 30_000)   // cap at 30 s
  }

  ws.onerror = (e) => console.error("WS error", e)
}

connect()

Multiple Symbols

Each WebSocket connection subscribes to one symbol + timeframe pair. To track multiple feeds, open one connection per pair. There is no multiplexing inside a single connection.

💡Tip: Keep all connections alive simultaneously. Reuse them — do not open and close a new connection per candle frame. Repeated open/close cycles will exhaust your connection rate limits.
ws-multi.js
// JavaScript — multiple symbol subscriptions (one connection per symbol/TF pair)
const subscriptions = [
  { symbol: "XAUUSD", timeFrame: "M1" },
  { symbol: "BTCUSD", timeFrame: "M5" },
  { symbol: "EURUSD", timeFrame: "H1" },
]

const connections = subscriptions.map(({ symbol, timeFrame }) => {
  const ws = new WebSocket(
    `wss://api.realmarketapi.com/price?apiKey=YOUR_API_KEY&symbolCode=${symbol}&timeFrame=${timeFrame}`
  )
  ws.onmessage = ({ data }) => handleCandle(JSON.parse(data))
  return ws
})

function handleCandle(candle) {
  // dispatch to per-symbol state / chart
  console.log(candle.SymbolCode, candle.OpenTime, candle.ClosePrice)
}
▶️

Test multi-symbol streaming

The Playground lets you stream multiple symbols at once and inspect every incoming frame in real-time.

Open Playground