# Streaming connection lost 1006 no reason

**URL:** <https://forum.alpaca.markets/t/streaming-connection-lost-1006-no-reason/6342>\
**Category:** Alpaca Market Data\
**Created:** [July 28, 2021, 10:41am UTC](https://forum.alpaca.markets/t/streaming-connection-lost-1006-no-reason/6342 "2021-07-28T10:41:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![orie](https://avatars.discourse-cdn.com/v4/letter/o/d2c977/32.png) [@orie](https://forum.alpaca.markets/u/orie)\
**Post date:** [July 28, 2021, 10:41am UTC](https://forum.alpaca.markets/t/streaming-connection-lost-1006-no-reason/6342/1 "2021-07-28T10:41:58Z")

</div>

Hi,

I have connected to the Alpaca streaming data API as shown here:

> **[Real-time data - Documentation | Alpaca](https://alpaca.markets/docs/api-documentation/api-v2/market-data/alpaca-data-api-v2/real-time/)**
>
> Alpaca API lets you build and trade with real-time market data for free.

I’ve used the Python SDK this way:

```auto
feed = 'sip'
stream = Stream(ALPACA_API_KEY,
                ALPACA_SECRET_KEY,
                data_feed=feed,
                raw_data=True)

for symbol, handler in zip(symbols, handlers):
    stream.subscribe_trades(handler.handle, symbol)

stream.run()

```

Every handler I attached reads the data from the message received and saves it as a property of a class, this is how the method looks like in my handler class:

```auto
    async def handle(self, msg):
        symbol = msg['S']
        if symbol == self.symbol:
            self._quote_time = datetime.datetime.fromtimestamp(
                msg['t'].seconds)
            self._last = msg['p']
            self._volume = msg['s']

            print(self.symbol, self.quote_time, self.last, self.volume)

```

If I leave the stream on, at some point I’m getting a “connection error 1006 no reason”

There is no algorithm running on the handler right now, if I add in `time.sleep(2)` after the printing to simulate a trading algorithm doing its work, I get the 1006 sooner.

The same problem was occuring when I used TD Ameritrade’s free streaming API.

How can I consume this market data without getting disconnected?

Thx,  
Orie

---

<div class="post-metadata">

**Author:** ![dhoehl](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dhoehl](https://forum.alpaca.markets/u/dhoehl)\
**Post date:** [July 29, 2021, 3:04pm UTC](https://forum.alpaca.markets/t/streaming-connection-lost-1006-no-reason/6342/2 "2021-07-29T15:04:11Z")

</div>

I am getting 1006 once in a while (several times a day, too)… Just adjust your program to automatically reconnect on 1006. Even if they fix this, your program can break at any time and should be able to reconnect automatically anyway

---

<div class="post-metadata">

**Author:** ![orie](https://avatars.discourse-cdn.com/v4/letter/o/d2c977/32.png) [@orie](https://forum.alpaca.markets/u/orie)\
**Post date:** [August 1, 2021, 7:43am UTC](https://forum.alpaca.markets/t/streaming-connection-lost-1006-no-reason/6342/3 "2021-08-01T07:43:39Z")

</div>

> [@dhoehl](#):
>
> ay

How would you implement such a function?
