Class WebSocketFeed

java.lang.Object
com.quantfinlib.feed.WebSocketFeed
All Implemented Interfaces:
AutoCloseable

public final class WebSocketFeed extends Object implements AutoCloseable
Live market data over WebSocket into the HftMarketDataBus — the last mile that lets the capture/replay, paper-trading and analytics stack run on real ticks. Pure JDK (java.net.http WebSocket client), pluggable FeedParser per exchange (BinanceTradeParser ships as the reference), automatic reconnection with exponential backoff, and an optional subscription message for exchanges that require one after connecting.

Wire once, and everything downstream just works:


 HftMarketDataBus bus = new HftMarketDataBus();
 TickCapture capture = TickCapture.attach(bus, Path.of("session.qflt"));
 bus.start();
 try (WebSocketFeed feed = new WebSocketFeed(
         BinanceTradeParser.streamUri("BTCUSDT", "ETHUSDT"),
         new BinanceTradeParser(), bus)) {
     feed.start();
     // strategies subscribe to the bus; the session is being recorded
 }
 

Threading: the JDK invokes WebSocket listener methods sequentially per connection, and reconnect epochs never overlap, so the bus's single-producer contract holds. Published tick timestamps are the exchange's event time (millis → nanos), so recorded sessions replay with true market pacing.

  • Constructor Details

  • Method Details

    • withSubscribeMessage

      public WebSocketFeed withSubscribeMessage(String message)
      Text frame sent right after connecting (exchanges with subscribe protocols).
    • withReconnect

      public WebSocketFeed withReconnect(int maxAttempts, long initialBackoffMillis)
    • start

      public void start() throws IOException
      Connects and blocks until the first connection is established.
      Throws:
      IOException
    • start

      public void start(Duration timeout) throws IOException
      Throws:
      IOException
    • isConnected

      public boolean isConnected()
    • messagesReceived

      public long messagesReceived()
    • tradesPublished

      public long tradesPublished()
    • reconnectCount

      public long reconnectCount()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable