Our WebSocket API v1 can be accessed via any WebSocket capable programming language (Python, Javascript, C#, Go, etc.). Many programming languages offer WebSocket libraries that allow programmers to use a WebSocket interface without understanding the intricate details of the WebSocket protocol.
Python is one example that offers many different WebSocket libraries, so how does a programmer know which library to use, or how to use their chosen library to best effectiveness. The following provides our recommended Python WebSocket library and gives some examples of how to use the library in different scenarios.
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 recommended 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 and installed simultaneously via the standard Python installation tool (pip):
- 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 a 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.kraken.com/")
ws.send('{"event":"subscribe", "subscription":{"name":"trade"}, "pair":["XBT/USD","XRP/USD"]}')
# 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('{"event":"subscribe", "subscription":{"name":"trade"}, "pair":["XBT/USD","XRP/USD"]}')
def ws_thread(*args):
ws = websocket.WebSocketApp("wss://ws.kraken.com/", 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())
Full details and instructions for the WebSocket client library are available via the library's code archive.