Python Websockets

Introduction

Our WebSocket API can be accessed via any WebSocket capable programming language (Python, Javascript, and so on). Many programming languages offer WebSocket libraries that allow programmers to use a WebSocket interface without understanding the intricate details of the WebSocket protocol.

WebSocket Client

Our recommended Python WebSocket library is the WebSocket-client library. The library is compatible with both Python 2 and Python 3, but for new code, we recommend only using Python 3 as Python 2 is in the process of being deprecated.

The WebSocket-client library can be downloaded from the Python Package Index (pypi.org) and installed via the included setup.py file:

  • python setup.py install

or downloaded/installed simultaneously via the standard Python installation tool (pip) which we recommend:

  • pip install websocket-client

Usage

The WebSocket client library can be used to create a synchronous (blocking) WebSocket client or an asynchronous (non-blocking, event-driven) client. Both versions can interact with our API successfully, so the choice would depend upon the specific requirements of the implementation (such as whether other tasks needed to happen in parallel).

For example, a simple market price watching app would be able to use a synchronous client that simply waited for new market data and compared the market price to a specified value, while a full-featured trading bot would need to use an asynchronous client with the market data feeds in separate threads (so that the analysis and trading tasks could continue in parallel).

The following are basic examples of both synchronous and asynchronous clients, which can be used as a starting point for more complex API code.

 

Synchronous WebSocket API Interface

 

# Import WebSocket client library from websocket import create_connection # Connect to WebSocket API and subscribe to trade feed for XBT/USD and XRP/USD ws = create_connection("wss://ws-sandbox.midchains.com:8081/") ws.send('''{ "type": "subscribe", "request": [ { "msg":"book", "security":"BCHUSD", "dest":"CROX" } ] }''') # Infinite loop waiting for WebSocket data while True: print(ws.recv())

 

Asynchronous WebSocket API Interface

 

# Import WebSocket client library (and others) import websocket import _thread import time # Define WebSocket callback functions def ws_message(ws, message): print("WebSocket thread: %s" % message) def ws_open(ws): ws.send('''{ "type": "subscribe", "request": [ { "msg":"book", "security":"BCHUSD", "dest":"CROX" } ] }''') def ws_thread(*args): ws = websocket.WebSocketApp("wss://ws-sandbox.midchains.com:8081/", on_open = ws_open, on_message = ws_message) ws.run_forever() # Start a new thread for the WebSocket interface _thread.start_new_thread(ws_thread, ()) # Continue other (non WebSocket) tasks in the main thread while True: time.sleep(5) print("Main thread: %d" % time.time())

 

More details are available about the WebSocket library via the library's code archive.