# Using Websockets

**URL:** <https://forum.alpaca.markets/t/using-websockets/4357>\
**Category:** Alpaca Trading\
**Created:** [February 9, 2021, 12:55am UTC](https://forum.alpaca.markets/t/using-websockets/4357 "2021-02-09T00:55:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayme](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jayme](https://forum.alpaca.markets/u/jayme)\
**Post date:** [February 9, 2021, 12:55am UTC](https://forum.alpaca.markets/t/using-websockets/4357/1 "2021-02-09T00:55:06Z")

</div>

so I’m exploring the world of WebSockets and I’m having some trouble… I’ve been knocking my head against the wall just trying to even get connected… here is a copy of a script I followed from a youtube video and it worked in the video but not mine. every time I run the script it just disconnects. what am I doing wrong.

I have intentionally left out my API keys here…

```
import config
import websocket, json

def on_open(ws):
print("opened")
auth_data = {
    "action": "authenticate",
    "data": {"key_id": API_KEY, "secret_key": SECRET_KEY}
}

ws.send(json.dumps(auth_data))

listen_message = {"action": "listen", "data": {"streams": ["AM.TSLA"]}}

ws.send(json.dumps(listen_message))

def on_message(ws, message):
print("received a message")
print(message)

def on_close(ws):
print("closed connection")

socket = "wss://data.alpaca.markets/stream"

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

---

<div class="post-metadata">

**Author:** ![efeineis](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@efeineis](https://forum.alpaca.markets/u/efeineis)\
**Post date:** [February 9, 2021, 4:06pm UTC](https://forum.alpaca.markets/t/using-websockets/4357/2 "2021-02-09T16:06:51Z")

</div>

You don’t get a Python interpreter error, do you? When I test your code, I get some output (ref attached screenshot). Is that what you get?

![python_websocket](https://us1.discourse-cdn.com/flex020/uploads/alp/original/2X/b/b1259976db90c9713c27a4740a69c83169a69d12.png)

---

<div class="post-metadata">

**Author:** ![efeineis](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@efeineis](https://forum.alpaca.markets/u/efeineis)\
**Post date:** [February 9, 2021, 4:07pm UTC](https://forum.alpaca.markets/t/using-websockets/4357/3 "2021-02-09T16:07:37Z")

</div>

If not, make sure you’ve installed the right websocket library. You want to install ‘websock-client’ …not just ‘websocket’

---

<div class="post-metadata">

**Author:** ![jayme](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jayme](https://forum.alpaca.markets/u/jayme)\
**Post date:** [February 11, 2021, 1:01am UTC](https://forum.alpaca.markets/t/using-websockets/4357/5 "2021-02-11T01:01:32Z")

</div>

well, it turns I had to run some Certificates command on my computer… _facepalm_ Thanks for the help guys.
