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.