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.
/price
Opens a persistent WebSocket connection. The server pushes a new OHLCV frame after each completed candle. Requires a Pro or Business plan.
| Name | Type | Description |
|---|---|---|
| apiKey* | string | Your API key. |
| symbolCode* | string | Symbol to stream (e.g. XAUUSD, BTCUSD). |
| timeFrame* | string | Candle interval: M1 ยท M5 ยท M15 ยท H1 ยท H4 ยท D1. |
Connection URL
CONNECTwss://api.realmarketapi.com/price?apiKey=YOUR_API_KEY&symbolCode=XAUUSD&timeFrame=M1
Incoming 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.
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.
// 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
onopenevent.
// 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.
// 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.