# How to reconnect to web sockets?

**URL:** <https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099>\
**Category:** Getting Started with Alpaca\
**Created:** [October 22, 2020, 6:23pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099 "2020-10-22T18:23:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex.meketer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/alex.meketer/32/1337_2.png) [@alex.meketer](https://forum.alpaca.markets/u/alex.meketer)\
**Post date:** [October 22, 2020, 6:23pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/1 "2020-10-22T18:23:05Z")

</div>

After some time connection to alpaca web socket drops.

How to reconnect or check if connection exists?

```
I used this:  
setInterval(async () => {
 updates_client.connect();
 }, 600000); 

```

But after some time it gives me an error.  
throw new Error(‘WebSocket is not open: readyState 0 (CONNECTING)’);  
^

```
Error: WebSocket is not open: readyState 0 (CONNECTING)

```

Can anybody give me an example of reconnection in Node please?

---

<div class="post-metadata">

**Author:** ![Shlomik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/shlomik/32/793_2.png) [@Shlomik](https://forum.alpaca.markets/u/Shlomik)\
**Post date:** [October 23, 2020, 4:10am UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/2 "2020-10-23T04:10:00Z")

</div>

Hi,  
try using this sample code: [https://github.com/alpacahq/alpaca-trade-api-js/blob/master/examples/websocket\_listener.js](https://github.com/alpacahq/alpaca-trade-api-js/blob/master/examples/websocket_listener.js)

---

<div class="post-metadata">

**Author:** ![Zach\_Oakes](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/zach_oakes/32/482_2.png) [@Zach\_Oakes](https://forum.alpaca.markets/u/Zach_Oakes)\
**Post date:** [October 29, 2020, 6:51am UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/3 "2020-10-29T06:51:19Z")

</div>

I wrote a reconnect block in Python — basically just re-initializes a connection if connection object is null (or if a basic socket operation fails) prior to any further requests.

> Blockquote

```
        if RE_CONN:
            try:
                  portfolio = self.api.list_positions()
             except:
                  print('Reconnecting...')
                  self.api = tradeapi.REST(base_url = ep, key_id = api_key, secret_key=secret_key)
              
                  portfolio = self.api.list_positions()

         else:
              portfolio = self.api.list_positions()

```

Cannot get formatting to work from mobile — sorry.  
It’s in my GitHub Trailstop code — this worked effectively for me on both live and paper environments.

> <https://github.com/zoakes/Auto_PM/blob/master/alpaca_trail_cat.py>

---

<div class="post-metadata">

**Author:** ![Shlomik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/shlomik/32/793_2.png) [@Shlomik](https://forum.alpaca.markets/u/Shlomik)\
**Post date:** [October 29, 2020, 4:43pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/4 "2020-10-29T16:43:42Z")

</div>

Hi you are doing REST api calls. you don’t need to reconnect.  
reconnection is for websockets

---

<div class="post-metadata">

**Author:** ![Zach\_Oakes](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/zach_oakes/32/482_2.png) [@Zach\_Oakes](https://forum.alpaca.markets/u/Zach_Oakes)\
**Post date:** [October 29, 2020, 5:05pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/5 "2020-10-29T17:05:11Z")

</div>

Regardless, it is still managed as a client basically, still loses connection to the REST, and still needs to be reconnected.

Typically this class instance maintains the connection, but sometimes throughout the day it’s lost and needs to re-init.

---

<div class="post-metadata">

**Author:** ![Shlomik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/shlomik/32/793_2.png) [@Shlomik](https://forum.alpaca.markets/u/Shlomik)\
**Post date:** [October 29, 2020, 5:23pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/6 "2020-10-29T17:23:25Z")

</div>

so I’m not sure I understand.  
doing a REST call always disconnects once it’s completed. it doesn’t stay connected.  
every time that you do something like `self.api.list_positions()` you connect and disconnect.  
so what do you mean by staying connected?

---

<div class="post-metadata">

**Author:** ![Zach\_Oakes](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/zach_oakes/32/482_2.png) [@Zach\_Oakes](https://forum.alpaca.markets/u/Zach_Oakes)\
**Post date:** [October 29, 2020, 5:46pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/7 "2020-10-29T17:46:01Z")

</div>

Yes, the list\_positions is a new call, however the connection to the API to GET the REST call is treated like a socket. The api class instance loses it’s connection, so the calls will start to fail, and need to be re-init.

---

<div class="post-metadata">

**Author:** ![Zach\_Oakes](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/zach_oakes/32/482_2.png) [@Zach\_Oakes](https://forum.alpaca.markets/u/Zach_Oakes)\
**Post date:** [October 29, 2020, 5:50pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/8 "2020-10-29T17:50:27Z")

</div>

Basically, self.api starts throwing errors, is no longer receiving anything from the REST. There’s alot of traffic to the API, so while the connection to the API should hold, and does many days, sometimes it loses connection like every hour or so — so the API connection needs to be re-initialized, as does the class/member variable used to reference it.

---

<div class="post-metadata">

**Author:** ![Shlomik](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/shlomik/32/793_2.png) [@Shlomik](https://forum.alpaca.markets/u/Shlomik)\
**Post date:** [October 29, 2020, 6:13pm UTC](https://forum.alpaca.markets/t/how-to-reconnect-to-web-sockets/3099/9 "2020-10-29T18:13:32Z")

</div>

this is not a socket. the REST class is an instance. it is not connected to the server.  
only when you do the rest call you connect to the server.

but you’re saying that the instance stops working? not receiving response from the server?  
are you maybe exceeding the limitations of the api calls?  
what error do you get?
