Class AlpacaStockStream

All Implemented Interfaces:
AutoCloseable

public final class AlpacaStockStream extends AbstractMarketDataStream
WebSocket client for the Alpaca real-time stock pricing stream (/v2/{source}).

Usage


 var creds  = new AlpacaCredentials(keyId, secretKey);
 var stream = AlpacaClientFactory.stockStream(
     creds,
     StockSource.IEX,
     AlpacaStreamEnvironment.PRODUCTION,
     new StockStreamListener() {
         @Override public void onTrade(StockTrade t) { System.out.println(t); }
     });

 stream.connect(StockSubscription.builder().trades("AAPL").quotes("AAPL").build());

 // Later:
 stream.close();
 

The client automatically reconnects with jittered exponential backoff on unexpected disconnects and re-subscribes to the previously confirmed symbol set after re-authentication.

Callbacks are dispatched on OkHttp's reader thread by default. Use the overloads that accept an Executor to offload listener work.