Skip to main content

WebSocket Connections

Aries provides real-time WebSocket APIs for streaming market data, account updates, and other time-sensitive information.

Available WebSocket Endpoints

Aries uses two WebSocket endpoints: one for market data and one for account-related streams.

Market Endpoint

Market Data WebSocket

Real-time equity quotes, options data, time & sales, level 2 order book, and market status. Authentication required.Endpoint: wss://api.aries.com/v1/market/ws

Charting WebSocket

Real-time quotes and trades for charting (TradingView-compatible). Authentication required (send token via POST /auth after connect).Endpoint: wss://api.aries.com/v1/charts/ws

Accounts Endpoint

Account Updates WebSocket

Orders, positions, balances, P&L candles, watchlist updates, and news. All account-related streams use this endpoint.Endpoint: wss://api.aries.com/v1/accounts/ws

Watchlist WebSocket

Real-time watchlist changes. Connect to the accounts endpoint and subscribe to the watchlist topic.Endpoint: wss://api.aries.com/v1/accounts/ws

WebSocket Features

Real-Time Data Streaming

All WebSocket connections support bi-directional communication for real-time data streaming with minimal latency.

Authentication

All WebSocket endpoints require authentication. After connecting, send your OAuth2 access token in a request message to POST /auth. Use type: "request", payload: { method: "post", path: "/auth", body: { action: "auth", token: "<access_token>" } }. Wait for authSuccess before subscribing.

Keep-Alive Support

WebSocket connections support ping/pong messages to maintain active connections and detect disconnections.

Selective Subscriptions

Subscribe only to the specific data you need to minimize bandwidth and processing overhead.

Connection Flow

1

Establish Connection

Connect to the WebSocket endpoint using the WSS protocol
2

Authenticate

Send authentication message with your OAuth2 access token
3

Subscribe

Subscribe to specific data streams or symbols
4

Receive Data

Process real-time data as it arrives
5

Maintain Connection

Send periodic ping messages to keep connection alive

Best Practices

Connection Management

Implement automatic reconnection logic with exponential backoff when connections drop:
let reconnectAttempts = 0;
const maxReconnectDelay = 30000;

function reconnect() {
  const delay = Math.min(1000 * Math.pow(2, reconnectAttempts), maxReconnectDelay);
  setTimeout(() => {
    reconnectAttempts++;
    connectWebSocket();
  }, delay);
}
Send ping messages regularly (recommended every 30-60 seconds) to maintain the connection:
const pingInterval = setInterval(() => {
  if (ws.readyState === WebSocket.OPEN) {
    ws.send(JSON.stringify({ action: "ping" }));
  }
}, 30000);
Implement proper error handling for connection failures, authentication errors, and data parsing issues:
ws.onerror = (error) => {
  console.error('WebSocket error:', error);
  // Log error details and attempt reconnection
  reconnect();
};
Always close WebSocket connections and clear intervals when they’re no longer needed:
function disconnect() {
  clearInterval(pingInterval);
  ws.close();
}

Performance Optimization

Selective Subscriptions

Only subscribe to symbols and fields you actively need to reduce bandwidth and processing overhead

Batch Updates

Process incoming messages in batches rather than individually for better performance

Connection Pooling

Reuse WebSocket connections when possible instead of creating new connections frequently

Message Throttling

Implement client-side throttling to handle high-frequency updates without overwhelming your UI

Quick Start Example

Here’s a basic example of connecting to the Market Data WebSocket:
const ws = new WebSocket('wss://api.aries.com/v1/market/ws');

ws.onopen = () => {
  // Authenticate
  ws.send(JSON.stringify({
    type: "request",
    id: "auth-001",
    payload: {
      method: "post",
      path: "/auth",
      body: {
        action: "auth",
        token: "YOUR_ACCESS_TOKEN"
      }
    }
  }));
};

ws.onmessage = (event) => {
  // Handle NDJSON: frames may contain multiple newline-delimited messages
  const lines = event.data.split('\n').filter(Boolean);
  for (const line of lines) {
    const message = JSON.parse(line);
    const payload = message.payload;

    if (payload?.action === "authSuccess") {
      // Subscribe to AAPL quotes after successful auth
      ws.send(JSON.stringify({
        type: "subscribe",
        id: "sub-1",
        payload: [{
          symbol: "AAPL",
          quoteFields: ["askPrice", "bidPrice", "lastPrice"]
        }]
      }));
    } else if (payload?.action === "update" && payload?.type === "quote") {
      // Process real-time quote data
      console.log('Quote update:', payload.symbol, payload.data);
    }
  }
};

ws.onerror = (error) => {
  console.error('WebSocket error:', error);
};

ws.onclose = () => {
  console.log('WebSocket connection closed');
};

Authentication

All WebSocket endpoints require authentication. After connecting, send your OAuth2 access token in a request message:
{
  "type": "request",
  "id": "auth-001",
  "payload": {
    "method": "post",
    "path": "/auth",
    "body": {
      "action": "auth",
      "token": "YOUR_ACCESS_TOKEN"
    }
  }
}
Obtain your access token through the OAuth2 flow. See the API References section for authentication details.

Support

Need help with WebSocket integration?

Email Support