WebSocket Public API Reference

The following is an API reference of CoPrA generated from Python source code and docstrings.

Warning

This is a complete reference of the public API of CoPrA. User code and applications should only rely on the public API, since internal APIs can (and will) change without any guarantees. Anything not listed here is considered a private API.

Module copra.websocket

class copra.websocket.Channel(name, product_ids)[source]

A WebSocket channel.

A Channel object encapsulates the Coinbase Pro WebSocket channel name and one or more Coinbase Pro product ids.

To read about Coinbase Pro channels and the data they return, visit: https://docs.gdax.com/#channels

Variables:
  • name (str) – The name of the WebSocket channel.
  • product_ids (set of str) – Product ids for the channel.
__init__(name, product_ids)[source]
Parameters:
  • name (str) – The name of the WebSocket channel. Possible values are heatbeat, ticker, level2, full, matches, or user
  • product_ids (str or list of str) – A single product id (eg., ‘BTC-USD’) or list of product ids (eg., [‘BTC-USD’, ‘ETH-EUR’, ‘LTC-BTC’])
Raises:

ValueError – If name not valid or product ids is empty.

class copra.websocket.Client(loop, channels, feed_url='wss://ws-feed.pro.coinbase.com:443', auth=False, key='', secret='', passphrase='', auto_connect=True, auto_reconnect=True, name='WebSocket Client')[source]

Asyncronous WebSocket client for Coinbase Pro.

__init__(loop, channels, feed_url='wss://ws-feed.pro.coinbase.com:443', auth=False, key='', secret='', passphrase='', auto_connect=True, auto_reconnect=True, name='WebSocket Client')[source]
Parameters:
  • loop (asyncio loop) – The asyncio loop that the client runs in.
  • channels (Channel or list of Channels) – The channels to initially subscribe to.
  • feed_url (str) – The url of the WebSocket server. The defualt is copra.WebSocket.FEED_URL (wss://ws-feed.gdax.com)
  • auth (bool) – Whether or not the (entire) WebSocket session is authenticated. If True, you will need an API key from the Coinbase Pro website. The default is False.
  • key (str) – The API key to use for authentication. Required if auth is True. The default is ‘’.
  • secret (str) – The secret string for the API key used for authenticaiton. Required if auth is True. The default is ‘’.
  • passphrase (str) – The passphrase for the API key used for authentication. Required if auth is True. The default is ‘’.
  • auto_connect (bool) – If True, the Client will automatically add itself to its event loop (ie., open a connection if the loop is running or as soon as it starts). If False, add_as_task_to_loop() needs to be explicitly called to add the client to the loop. The default is True.
  • auto_reconnect (bool) – If True, the Client will attemp to autom- matically reconnect and resubscribe if the connection is closed any way but by the Client explicitly itself. The default is True.
  • name (str) – A name to identify this client in logging, etc.
Raises:

ValueError – If auth is True and key, secret, and passphrase are not provided.

add_as_task_to_loop()[source]

Add the client to the asyncio loop.

Creates a coroutine for making a connection to the WebSocket server and adds it as a task to the asyncio loop.

close()[source]

Close the WebSocket connection.

on_close(was_clean, code, reason)[source]

Callback fired when the WebSocket connection has been closed.

(WebSocket closing handshake has been finished or the connection was closed uncleanly).

Parameters:
  • was_clean (bool) – True iff the WebSocket connection closed cleanly.
  • code (int or None) – Close status code as sent by the WebSocket peer.
  • reason (str or None) – Close reason as sent by the WebSocket peer.
on_error(message, reason='')[source]

Callback fired when an error message is received.

Parameters:
  • message (str) – A general description of the error.
  • reason (str) – A more detailed description of the error.
on_message(message)[source]

Callback fired when a complete WebSocket message was received.

You will likely want to override this method.

Parameters:message (dict) – Dictionary representing the message.
on_open()[source]

Callback fired on initial WebSocket opening handshake completion.

The WebSocket is open. This method sends the subscription message to the server.

subscribe(channels)[source]

Subscribe to the given channels.

Parameters:channels (Channel or list of Channels) – The channels to subscribe to.
unsubscribe(channels)[source]

Unsubscribe from the given channels.

Parameters:channels (Channel or list of Channels) – The channels to subscribe to.