# Websocket send after initial connection?

**URL:** <https://forum.alpaca.markets/t/websocket-send-after-initial-connection/2894>\
**Category:** Alpaca Market Data\
**Created:** [October 5, 2020, 8:42pm UTC](https://forum.alpaca.markets/t/websocket-send-after-initial-connection/2894 "2020-10-05T20:42:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Matthew](https://forum.alpaca.markets/u/Matthew)\
**Post date:** [October 5, 2020, 8:42pm UTC](https://forum.alpaca.markets/t/websocket-send-after-initial-connection/2894/1 "2020-10-05T20:42:08Z")

</div>

I’ve been using the code from: [https://github.com/hackingthemarkets/alpaca-websockets/blob/master/stream.py](https://github.com/hackingthemarkets/alpaca-websockets/blob/master/stream.py)

I can successfully authorize the connection and send an initial message to listen for some data:

```auto
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
ws.run_forever()

```

However, the thing that has me stumped is how do I send additional messages once the initial connection is made? The initial message is bundled inside the on\_open function, so I don’t really understand how to send additional message after that first on\_open message is sent. For example, if I try to do this:

```auto
message = {"action": "listen", "data": {"streams": ["AM.TSLA", "T.TSLA"]}}
ws.send(json.dumps(message))

```

it doesn’t work. Nothing seems to happen and I don’t get any response. The only solution I have right now is to close the original connection, and create a new one with a different message, but that just seems dumb.

---

<div class="post-metadata">

**Author:** ![ZippeyKeys12](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/zippeykeys12/32/1370_2.png) [@ZippeyKeys12](https://forum.alpaca.markets/u/ZippeyKeys12)\
**Post date:** [October 5, 2020, 11:37pm UTC](https://forum.alpaca.markets/t/websocket-send-after-initial-connection/2894/2 "2020-10-05T23:37:04Z")

</div>

Why not use the stream API in the official SDK?  
It’d let you listen to those endpoints using a decorator.

> **[alpacahq/alpaca-trade-api-python](https://github.com/alpacahq/alpaca-trade-api-python#streamconn)**
>
> Python client for Alpaca's trade API. Contribute to alpacahq/alpaca-trade-api-python development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Matthew](https://forum.alpaca.markets/u/Matthew)\
**Post date:** [October 6, 2020, 2:20am UTC](https://forum.alpaca.markets/t/websocket-send-after-initial-connection/2894/3 "2020-10-06T02:20:51Z")

</div>

thanks for the reply. I guess it’s because I don’t really understand how it works. But I guess that’s not saying much considering I don’t know how the web socket works either, lol.

But i think you’re right, maybe I should try that out

---

<div class="post-metadata">

**Author:** ![ZippeyKeys12](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/zippeykeys12/32/1370_2.png) [@ZippeyKeys12](https://forum.alpaca.markets/u/ZippeyKeys12)\
**Post date:** [October 6, 2020, 8:12pm UTC](https://forum.alpaca.markets/t/websocket-send-after-initial-connection/2894/4 "2020-10-06T20:12:53Z")

</div>

The official API is definitely an easier way to go, hope you get everything working!
