Skip to Content
Usage ExamplesWebsockets

WebSockets

The Nord application provides real-time data streaming via WebSocket connections. This allows for efficient updates of market data such as candle charts.

Chart Data Streaming

The application uses a dedicated WebSocket endpoint to stream real-time candle data for charts.

Nord API URLs:

  • Mainnet: wss://zo-mainnet.n1.xyz
  • Devnet: wss://zo-devnet.n1.xyz

Endpoint

The WebSocket URL is constructed using the base Nord URL extended with the specific symbol and resolution.

WS_URL = {NORD_API_URL}/ws/candle@{symbol}:{resolution}
  • symbol: The trading pair symbol (e.g., SOLUSD, BTCUSD).
  • resolution: The chart resolution timeframe (e.g., 1, 5, 60, 1D).

Connection Lifecycle

Each WebSocket connection is dedicated to a specific symbol and resolution pair.

  1. Initialization: When a chart component mounts or the user changes the symbol/resolution, a new WebSocket connection is established.
  2. Multiplexing: The client implementation (streaming.ts) handles multiplexing, allowing multiple subscribers (e.g., different UI components) to listen to the same data stream without opening duplicate sockets.
  3. Cleanup: Connections are automatically closed when no subscribers remain for a specific channel or when switching symbols, ensuring efficient resource usage.

Message Format

The server sends JSON-formatted messages representing candle updates. The CandleUpdate interface is defined as follows:

interface CandleUpdate { res: string; // Resolution of the candle data (e.g., "1", "1D") mid: number; // Market identifier t: number; // Timestamp in seconds o: number; // Open price h: number; // High price l: number; // Low price c: number; // Close price v?: number; // Volume (optional) }

Usage Example

The following example demonstrates how to establish a connection and handle incoming messages.

// Define the Nord API URLs for different environments const NORD_URL_MAINNET = "wss://zo-mainnet.n1.xyz"; const NORD_URL_DEVNET = "wss://zo-devnet.n1.xyz"; // Select the environment const nordUrl = NORD_URL_MAINNET; const symbol = "SOLUSD"; const resolution = "1"; const url = `${nordUrl}/ws/candle@${symbol}:${resolution}`; const socket = new WebSocket(url); socket.onopen = () => { console.log(`Connected to ${symbol} ${resolution} stream`); }; socket.onmessage = (event) => { const data: CandleUpdate = JSON.parse(event.data); console.log("New candle data:", data); // Process data (e.g., update chart) }; socket.onclose = (event) => { console.log("Connection closed", event.code, event.reason); }; socket.onerror = (event) => { console.error("WebSocket error", event); };
Last updated on