Skip to content

Instantly share code, notes, and snippets.

@tonyroberts
Created October 27, 2020 10:10
Show Gist options
  • Save tonyroberts/d5e9cc4bf6f512b2d3e2fd9097c088ac to your computer and use it in GitHub Desktop.
Save tonyroberts/d5e9cc4bf6f512b2d3e2fd9097c088ac to your computer and use it in GitHub Desktop.
import websockets
import asyncio
import json
async def main():
uri = "wss://www.bitmex.com/realtime"
async with websockets.connect(uri) as websocket:
# Send a message to subscribe to XBTUSD updates
msg = {
"op": "subscribe",
"args": ["instrument:XBTUSD"]
}
await websocket.send(json.dumps(msg))
# Receive messages in a loop and print them out
while True:
result = await websocket.recv()
data = json.loads(result)
print(data)
if __name__ == "__main__":
# Run the 'main' function in an asyncio event loop
loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment