# WS Listen, seems to hang

**URL:** <https://community.tempest.earth/t/ws-listen-seems-to-hang/11953>\
**Category:** Developers\
**Tags:** tempest\
**Created:** [January 25, 2021, 11:22pm UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953 "2021-01-25T23:22:22Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfaith5125](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@sfaith5125](https://community.tempest.earth/u/sfaith5125)\
**Post date:** [January 25, 2021, 11:22pm UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/1 "2021-01-25T23:22:23Z")

</div>

I am using the following in Python

print(‘Opening Websocket connection…’)  
ws = create\_connection(‘wss://ws.weatherflow.com/swd/data?api\_key=’ + personal\_token)  
result = ws.recv()  
print(“Received ‘%s’” % result)  
print(’’)

print("Listening to Tempest endpoint… " )  
ws.send(’{“type”:“listen\_start”,’ + ’ “device\_id”:’ + tempest\_ID + ‘,’ + ’ “id”:“2098388936”}’)  
result = ws.recv()  
print(“Received ‘%s’” % result)  
print(’’)

It clearly opens the connection but when I send the listen command, after 10 minutes nothing is received.

Am I missing something?

---

<div class="post-metadata">

**Author:** ![vinceskahan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/vinceskahan/32/2785_2.png) [@vinceskahan](https://community.tempest.earth/u/vinceskahan)\
**Post date:** [January 26, 2021, 2:17am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/2 "2021-01-26T02:17:16Z")

</div>

That device id sure looks wrong to me. Has WF sold over 2 billion tempests ?

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [January 26, 2021, 2:22am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/3 "2021-01-26T02:22:54Z")

</div>

That number has a different meaning.

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [January 26, 2021, 3:28am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/4 "2021-01-26T03:28:04Z")

</div>

> [@sfaith5125](#):
>
> Am I missing something?

What is the value of tempest\_ID?

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [January 26, 2021, 3:34am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/5 "2021-01-26T03:34:52Z")

</div>

> [@sfaith5125](#):
>
> ws.send(’{“type”:“listen\_start”,’ + ’ “device\_id”:’ + tempest\_ID + ‘,’ + ’ “id”:“2098388936”}’)

Try this:

ws.send({“type”:“listen\_start”,’  
“device\_id”:’ + tempest\_ID + ‘,“id”:“2098388936”})

---

<div class="post-metadata">

**Author:** ![peter](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/peter/32/1365_2.png) [@peter](https://community.tempest.earth/u/peter)\
**Post date:** [January 26, 2021, 8:53am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/6 "2021-01-26T08:53:32Z")

</div>

It looks like you _may_ have based your code off the snippet I shared here: [Basic Python websockets example to retrieve current Tempest data](https://community.tempest.earth/t/basic-python-websockets-example-to-retrieve-current-tempest-data/9310/). If that is the case, the code I shared was not designed to receive multiple messages from the Websocket, and the same is true in the code you shared. Once you have sent the listen command, `ws.recv()` is only called once, and therefore you will only receive a single message.

If you want to remain connected and listen to new messages continuously, you need to wrap `ws.recv()` inside a `while` loop. Something like this will work (don’t forget to enter values for the `personal_token` and `tempest_ID`):

```auto
from websocket import create_connection

personal_token = ' '
tempest_ID = ' '

print('Opening Websocket connection...')
ws = create_connection('wss://ws.weatherflow.com/swd/data?api_key=' + personal_token)
result = ws.recv()
print("Received '%s'" % result)
print('')

print('Listening to Tempest endpoint...')
ws.send('{"type":"listen_start",' + ' "device_id":' + tempest_ID + ', "id":"Tempest"}')
result = ws.recv()
print("Received '%s'" % result)
print('')

print('Receiving Tempest data...')
while True:
    result = ws.recv()
    print("Received '%s'" % result)
    print('')

```

---

<div class="post-metadata">

**Author:** ![sfaith5125](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@sfaith5125](https://community.tempest.earth/u/sfaith5125)\
**Post date:** [January 26, 2021, 11:15am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/7 "2021-01-26T11:15:21Z")

</div>

Got it thanks and yes I did base it off of your code

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [January 26, 2021, 12:43pm UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/8 "2021-01-26T12:43:34Z")

</div>

Thank you, Peter…

---

<div class="post-metadata">

**Author:** ![erholp](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@erholp](https://community.tempest.earth/u/erholp)\
**Post date:** [January 27, 2021, 11:07pm UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/9 "2021-01-27T23:07:32Z")

</div>

Sorry to butt in with a question from left field. Am I interpreting this code correctly when I ask: It appears to me that this is setting up a listening socket… this seems to imply that the device is sending data instead of reacting to a connection and sent data. Is this correct? Is the remote device (the weatherflow server) doing a push instead of the local device doing a pull, so to speak? If so, is there setup involved to make this happen?

Thank you!

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [January 28, 2021, 2:29am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/10 "2021-01-28T02:29:32Z")

</div>

The Hub transmits the data via UDP on the local network. That data is available for any device to listen to it.

---

<div class="post-metadata">

**Author:** ![erholp](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@erholp](https://community.tempest.earth/u/erholp)\
**Post date:** [January 28, 2021, 2:56am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/11 "2021-01-28T02:56:29Z")

</div>

Thank you Gary… out of curiosity, does the hub send the UDP packets or does the server at the weatherflow facility send the packets after communicating with the hub?

---

<div class="post-metadata">

**Author:** ![gizmoev](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/gizmoev/32/941_2.png) [@gizmoev](https://community.tempest.earth/u/gizmoev)\
**Post date:** [January 28, 2021, 3:05am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/12 "2021-01-28T03:05:38Z")

</div>

The hub sends the UDP packets. I had to tell my Wi-Fi access points to broadcast them so my Meteobridge and ArchiveWS on an RPi could receive them since they were not wired.

---

<div class="post-metadata">

**Author:** ![peter](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/peter/32/1365_2.png) [@peter](https://community.tempest.earth/u/peter)\
**Post date:** [January 28, 2021, 8:35am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/13 "2021-01-28T08:35:16Z")

</div>

> [@erholp](#):
>
> Am I interpreting this code correctly when I ask: It appears to me that this is setting up a listening socket…

The code snippet I shared above creates a client connection to the WeatherFlow websocket server (different to UDP). Once the connection is set up, new messages are pushed automatically to the client by the remote server. There is no ongoing requirement for the client to issue data requests to the server. If you want the client to use a pull model instead, then the REST API is the place to go. For both the Websocket and REST API the data flow is:

`Device (Tempest/Sky etc)` → `Hub` → (internet) → `WeatherFlow server` → (internet, push or pull) → `Client`

If you don’t want the internet to be involved at all, then instead you can listen for the UDP messages that are broadcast by the hub over the local network it is connected to. This is a push model similar to the Websocket. For UDP, the data flow is:

`Device (Tempest/Sky etc)` → `Hub` → (local network, push) → `Client`

---

<div class="post-metadata">

**Author:** ![GaryFunk](https://sea2.discourse-cdn.com/flex020/user_avatar/community.tempest.earth/garyfunk/32/5729_2.png) [@GaryFunk](https://community.tempest.earth/u/GaryFunk)\
**Post date:** [January 28, 2021, 10:46am UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/14 "2021-01-28T10:46:52Z")

</div>

> [@](#):
>
> The code snippet I shared above creates a client connection to the WeatherFlow websocket server

Yeah, what he said. My apology. I don’t know why I thought UDP when this topic is for the web socket. Peter gave you the correct information.

---

<div class="post-metadata">

**Author:** ![erholp](https://avatars.discourse-cdn.com/v4/letter/e/7ba0ec/32.png) [@erholp](https://community.tempest.earth/u/erholp)\
**Post date:** [January 28, 2021, 2:19pm UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/15 "2021-01-28T14:19:40Z")

</div>

Wow! Good info!!! Thanks @peter and others! I have not used websockets. I’d like to try out these things described here!

---

<div class="post-metadata">

**Author:** ![sfaith5125](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@sfaith5125](https://community.tempest.earth/u/sfaith5125)\
**Post date:** [January 28, 2021, 3:47pm UTC](https://community.tempest.earth/t/ws-listen-seems-to-hang/11953/16 "2021-01-28T15:47:39Z")

</div>

Please share what you have found to work.
