Reputation: 33
I'm fairly new to scripting in general and I'm pretty sure this is trivial but i can't seem to find a solution. I want to use the python websockets library to listen to multiple websockets in order to get ticker information about crypto prices.
How to get real time bid / ask / price from GDAX websocket feed provides a good start for obtaining the feed for one currency.
The problem is that the run_forever() does not allow me to show two feeds at the same time as i have no way to interrupt it.
Upvotes: 2
Views: 3346
Reputation: 1686
The GDAX websocket allows you to subscribe to multiple pairs.
As seen below I subscribe to both the BTC-USD
and ETH-USD
pairs. I assume you can subscribe to unlimited pairs.
import websocket
from json import dumps, loads
try:
import thread
except ImportError:
import _thread as thread
def on_message(ws, message):
parsed_msg = loads(message)
print(parsed_msg["product_id"], parsed_msg["price"])
def on_open(ws):
def run(*args):
params = {
"type": "subscribe",
"channels": [{"name": "ticker", "product_ids": ["BTC-USD", "ETH-USD"]}]
}
ws.send(dumps(params))
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://ws-feed.gdax.com", on_open=on_open, on_message = on_message)
ws.run_forever()
If for some reason GDAX did not allow this, you could open multiple web sockets in multiple threads, but in this case its not necessary.
Upvotes: 3