> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pragma.build/llms.txt
> Use this file to discover all available pages before exploring further.

# Lightspeed

> Ultra-fast price data streaming without verification overhead

<Card title="High-Performance Price Streaming" icon="bolt" horizontal>
  The fastest and most accurate market data with minimal overhead
</Card>

## Overview

<Info>
  Lightspeed is optimized for performance, delivering price updates every 500 milliseconds without the verification overhead of our other endpoints.
</Info>

This endpoint is very similar to our [StarkEx](./starkex) endpoint except it doesn't contain any metadata that would allow you to verify data's provenance. The trade-off provides significantly improved performance by eliminating signature generation overhead.

<Tabs>
  <Tab title="Key Features">
    <CardGroup cols={3}>
      <Card title="Ultra-Low Latency" icon="bolt">
        Updates every 500ms with minimal processing overhead
      </Card>

      <Card title="High Accuracy" icon="bullseye">
        Aggregated from multiple premium sources
      </Card>

      <Card title="Simplified Format" icon="arrows-minimize">
        Streamlined JSON responses for easier integration
      </Card>
    </CardGroup>
  </Tab>

  <Tab title="Use Cases">
    <CardGroup cols={2}>
      <Card title="Trading Applications" icon="chart-line">
        Perfect for applications where microseconds matter and external verification isn't required
      </Card>

      <Card title="Market Monitoring" icon="eye">
        Real-time dashboard applications requiring immediate price updates
      </Card>

      <Card title="Alert Systems" icon="bell">
        Price notification systems that need to react instantly to market changes
      </Card>

      <Card title="Performance-First Systems" icon="gauge-high">
        Systems where latency is the primary concern over provenance verification
      </Card>
    </CardGroup>
  </Tab>
</Tabs>

## Connection

<Steps>
  <Step title="Open WebSocket Connection">
    Connect to our WebSocket endpoint on your preferred environment:

    <CodeGroup>
      ```bash Development theme={null}
      wscat -c wss://ws.devnet.pragma.build/node/v1/data/price/subscribe
      ```

      ```bash Production theme={null}
      wscat -c wss://ws.production.pragma.build/node/v1/data/price/subscribe
      ```
    </CodeGroup>
  </Step>

  <Step title="Subscribe to Asset Pairs">
    By default, no asset pairs are subscribed. Send a subscription message to start receiving data.
  </Step>

  <Step title="Receive Real-Time Updates">
    Once subscribed, you'll receive price updates every 500 milliseconds for your selected pairs.
  </Step>
</Steps>

## Subscription Management

### Subscribing to Pairs

<CodeGroup>
  ```json Request theme={null}
  {
    "msg_type": "subscribe",
    "pairs": ["BTC/USD", "ETH/USD:MARK"]
  }
  ```

  ```json Response theme={null}
  {
    "msg_type": "subscribe",
    "pairs": ["BTC/USD", "ETH/USD:MARK", "SOL/USD"]
  }
  ```
</CodeGroup>

<Note>
  The response includes all pairs you are currently subscribed to, including any previous subscriptions not mentioned in your latest request.
</Note>

### Unsubscribing from Pairs

<CodeGroup>
  ```json Request theme={null}
  {
    "msg_type": "unsubscribe",
    "pairs": ["BTC/USD"]
  }
  ```

  ```json Response theme={null}
  {
    "msg_type": "unsubscribe",
    "pairs": ["ETH/USD:MARK", "SOL/USD"]
  }
  ```
</CodeGroup>

<Warning>
  If you close your WebSocket connection, all your subscriptions will be automatically cleared. You'll need to resubscribe when reconnecting.
</Warning>

## Data Format

<CodeGroup>
  ```json Example Price Update theme={null}
  {
    "oracle_prices": [
      {
        "num_sources_aggregated": 12,
        "pair_id": "BTC/USD",
        "price": "114876664243612000000000"
      }
    ],
    "timestamp": 1757944509818
  }
  ```
</CodeGroup>

### Field Reference

<Properties>
  <Property name="oracle_prices" type="array">
    Array containing price data for subscribed asset pairs.
  </Property>

  <Property name="oracle_prices[].pair_id" type="string">
    The asset pair identifier (e.g., "BTC/USD").
  </Property>

  <Property name="oracle_prices[].price" type="string">
    The median price of the asset represented as a string to preserve precision.
  </Property>

  <Property name="oracle_prices[].num_sources_aggregated" type="number">
    The number of sources that were aggregated for computing the median price.
  </Property>

  <Property name="timestamp" type="number">
    Unix timestamp in milliseconds when the price update was generated.
  </Property>
</Properties>

<Tip>
  All numeric values are returned as strings to preserve precision, especially for assets with very small or very large prices.
</Tip>

## Implementation Examples

<Tabs>
  <Tab title="JavaScript">
    ```javascript example.js [expandable] theme={null}
    const WebSocket = require('ws');

    // Connect to Lightspeed endpoint
    const ws = new WebSocket('wss://ws.devnet.pragma.build/node/v1/data/price/subscribe');

    // Subscribe to pairs on connection open
    ws.on('open', function open() {
      console.log('Connected to Lightspeed API');
      
      // Subscribe to specific pairs
      const subscribeMsg = {
        msg_type: "subscribe",
        pairs: ["BTC/USD", "ETH/USD"]
      };
      
      ws.send(JSON.stringify(subscribeMsg));
    });

    // Handle incoming messages
    ws.on('message', function incoming(data) {
      const message = JSON.parse(data);
      
      // Handle subscription confirmations
      if (message.msg_type === "subscribe" || message.msg_type === "unsubscribe") {
        console.log(`${message.msg_type} confirmation:`, message.pairs);
        return;
      }
      
      // Handle price updates
      if (message.oracle_prices) {
        message.oracle_prices.forEach(price => {
          console.log(`Price update for ${price.pair_id}:`, {
            price: price.price,
            sources: price.num_sources_aggregated,
            timestamp: message.timestamp
          });
        });
      }
      
      // Process price data as needed
      // ...
    });

    // Handle errors and disconnections
    ws.on('error', function error(err) {
      console.error('Connection error:', err);
    });

    ws.on('close', function close() {
      console.log('Connection closed');
    });
    ```
  </Tab>

  <Tab title="Python">
    ```python example.py [expandable] theme={null}
    import json
    import websocket

    # Define callbacks
    def on_message(ws, message):
        data = json.loads(message)
        
        # Handle subscription confirmations
        if data.get('msg_type') in ['subscribe', 'unsubscribe']:
            print(f"{data['msg_type']} confirmation: {data['pairs']}")
            return
            
        # Handle price updates
        if 'oracle_prices' in data:
            for price in data['oracle_prices']:
                print(f"Price update for {price['pair_id']}: {price['price']} (from {price['num_sources_aggregated']} sources, timestamp: {data['timestamp']})")
        
        # Process price data as needed
        # ...

    def on_error(ws, error):
        print(f"Error: {error}")

    def on_close(ws, close_status_code, close_msg):
        print(f"Connection closed: {close_status_code} - {close_msg}")

    def on_open(ws):
        print("Connection established")
        
        # Subscribe to specific pairs
        subscribe_msg = {
            "msg_type": "subscribe",
            "pairs": ["BTC/USD", "ETH/USD"]
        }
        
        ws.send(json.dumps(subscribe_msg))

    # Connect to Lightspeed endpoint
    if __name__ == "__main__":
        websocket.enableTrace(True)  # For debugging
        ws = websocket.WebSocketApp("wss://ws.devnet.pragma.build/node/v1/data/price/subscribe",
                                   on_open=on_open,
                                   on_message=on_message,
                                   on_error=on_error,
                                   on_close=on_close)
        
        ws.run_forever()
    ```
  </Tab>
</Tabs>

## Best Practices

<CardGroup cols={2}>
  <Card title="Implement Reconnection Logic" icon="rotate">
    WebSocket connections can drop for various reasons. Always implement automatic reconnection with backoff strategy.
  </Card>

  <Card title="Process Updates Asynchronously" icon="gears">
    Handle price updates asynchronously to avoid blocking the WebSocket receive thread, especially in high-frequency scenarios.
  </Card>

  <Card title="Batch Subscription Changes" icon="layer-group">
    When subscribing to multiple pairs, send them in a single message rather than separate requests.
  </Card>

  <Card title="Monitor Connection Health" icon="heart-pulse">
    Implement periodic health checks and heartbeat monitoring to ensure your connection stays active.
  </Card>
</CardGroup>

## Comparison with Starkex

<CardGroup cols={2}>
  <Card title="Lightspeed" icon="bolt">
    <ul className="list-none space-y-2 mt-2">
      <li>✓ Minimal payload size</li>
      <li>✓ Optimized for performance</li>
      <li>✓ Lower latency</li>
      <li>✓ Ideal for trading applications</li>
      <li>❌ No verification metadata</li>
    </ul>
  </Card>

  <Card title="Starkex" icon="shield-check">
    <ul className="list-none space-y-2 mt-2">
      <li>✓ Includes cryptographic signatures</li>
      <li>✓ Verifiable price data</li>
      <li>✓ Individual publisher prices</li>
      <li>✓ Ideal for StarkEx integration</li>
      <li>⚠️ Slightly larger payload size</li>
    </ul>
  </Card>
</CardGroup>

<Tip>
  Choose Lightspeed when performance is your primary concern and Starkex when data provenance verification is required.
</Tip>
