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