Class AbstractMarketDataStream

java.lang.Object
markets.alpaca.client.ws.internal.AbstractAlpacaStream
markets.alpaca.client.ws.internal.AbstractMarketDataStream
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
AlpacaCryptoStream, AlpacaNewsStream, AlpacaStockStream

public abstract class AbstractMarketDataStream extends AbstractAlpacaStream
Shared protocol logic for the three market-data WebSocket streams (stock, crypto, and news).

All three streams follow the same message envelope:

  1. Server sends [{"T":"success","msg":"connected"}].
  2. Client sends {"action":"auth","key":"...","secret":"..."}.
  3. Server sends [{"T":"success","msg":"authenticated"}].
  4. Client sends subscribe/unsubscribe messages.
  5. Server streams data as JSON arrays; each element has a T discriminator.

This class handles the auth/connected handshake, subscription state tracking (updated from confirmed subscription messages), and reconnect-aware re-subscription. Subclasses implement data-message dispatch via dispatchDataMessage(com.google.gson.JsonObject, java.lang.String).

  • Field Details

    • GSON

      protected static final com.google.gson.Gson GSON
  • Constructor Details

  • Method Details

    • handleOpen

      protected void handleOpen(okhttp3.WebSocket ws)
      Description copied from class: AbstractAlpacaStream
      Called when the WebSocket connection is open.
      Specified by:
      handleOpen in class AbstractAlpacaStream
    • handleText

      protected final void handleText(okhttp3.WebSocket ws, String text)
      Description copied from class: AbstractAlpacaStream
      Called for each text frame received from the server.
      Specified by:
      handleText in class AbstractAlpacaStream
    • handleClose

      protected final void handleClose(int code, String reason, boolean willReconnect)
      Description copied from class: AbstractAlpacaStream
      Called when the connection closes, either cleanly or due to an error.
      Specified by:
      handleClose in class AbstractAlpacaStream
      Parameters:
      code - WebSocket close code, or -1 for transport failures
      reason - human-readable close reason
    • getConfirmedSubscriptions

      protected Map<String,Set<String>> getConfirmedSubscriptions()
      Returns the current server-confirmed subscriptions, keyed by channel name.
    • sendSubscriptionMessage

      protected void sendSubscriptionMessage(String action, Map<String,Set<String>> payload)
      Sends a subscribe or unsubscribe message and returns immediately. The confirmed state is updated when the server responds with a subscription message.
      Parameters:
      action - "subscribe" or "unsubscribe"
      payload - channel-to-symbols map (channels with empty sets are omitted)
    • resubscribeAfterAuth

      protected void resubscribeAfterAuth(okhttp3.WebSocket ws)
      Called after (re-)authentication to re-subscribe with the previously confirmed state. Subclasses may override to add their own reconnect-subscribe payload building.
    • dispatchDataMessage

      protected abstract void dispatchDataMessage(com.google.gson.JsonObject msg, String type)
      Called for each data message element with a non-control T value.
      Parameters:
      msg - the JSON object for this message
      type - the T field value (e.g. "t", "q", "b")
    • notifyConnected

      protected abstract void notifyConnected()
    • notifyAuthenticated

      protected abstract void notifyAuthenticated()
    • notifySubscriptionConfirmed

      protected abstract void notifySubscriptionConfirmed(Map<String,List<String>> subscriptions)
    • notifyError

      protected abstract void notifyError(int code, String message)
    • handleMarketDataClose

      protected abstract void handleMarketDataClose(int code, String reason, boolean willReconnect)