Skip to content

Instantly share code, notes, and snippets.

@whittlem
Last active September 25, 2021 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whittlem/728b7081abddc51ede48fc8495455d24 to your computer and use it in GitHub Desktop.
Save whittlem/728b7081abddc51ede48fc8495455d24 to your computer and use it in GitHub Desktop.
import json, time
from threading import Thread
from websocket import create_connection, WebSocketConnectionClosedException
def main():
ws = None
thread = None
thread_running = False
thread_keepalive = None
def websocket_thread():
global ws
ws = create_connection("wss://ws-feed.pro.coinbase.com")
ws.send(
json.dumps(
{
"type": "subscribe",
"product_ids": ['BTC-USD'],
"channels": ["matches"],
}
)
)
thread_keepalive.start()
while not thread_running:
try:
data = ws.recv()
if data != "":
msg = json.loads(data)
else:
msg = {}
except ValueError as e:
print(e)
print("{} - data: {}".format(e, data))
except Exception as e:
print(e)
print("{} - data: {}".format(e, data))
else:
if "result" not in msg:
print(msg)
try:
if ws:
ws.close()
except WebSocketConnectionClosedException:
pass
finally:
thread_keepalive.join()
def websocket_keepalive(interval=30):
global ws
while ws.connected:
ws.ping("keepalive")
time.sleep(interval)
thread = Thread(target=websocket_thread)
thread_keepalive = Thread(target=websocket_keepalive)
thread.start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment