Streaming Observations

This may help folks trying to use streaming…
Platform: Windows 10, Visual Studio Community Edition 2019, .Net Framework 4.8, Alpaca SDK 4.0.1
Console app implementing asynchronous tasks
Paper Trading only (so far)
Implementing ‘AlpacaDataStreamingClient’, not ‘AlpacaStreamingClient’
Using events:
// OnError
// SocketOpened
// SocketClosed
// Connected
// …Received (for subscriptions)

After successfully creating instance of AlpacaDataStreamingClient, I define event handlers, connect and authenticate stream, create subscriptions, and subscription event hander.

I then spin up an asynchronous task that checks SocketClosed flag every 2.5 seconds. If socket is closed, I disconnect and reconnect stream. (SocketClosed flag is set when SocketClosed event fires). This works great when the only event fired is SocketClosed.

The kiss-of-death occurs when the OnError event fires. In this situation, disconnecting and reconnecting the stream does not good. You will need to recreate your instance of the AlpacaDataStreamingClient object and then define event handlers, connect and authenticate stream, create subscriptions, and subscription event hander… The error message received when the OnError event fires is “System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.” Once the error occurs, the socket cannot be reopened.

1 Like