Deephaven and asyncio for live crypto monitoring

Create streaming tables to capture Coinbase data

September 1 2023

Devin SmithDevin SmithDeveloper @Deephaven
JJ BrosnanJJ BrosnanDeveloper Relations Engineer @Deephaven
AI prompt: a large pile of gold bitcoins and a few solid plastic colorful cubes, isolated on a dark blue background

Last year, our colleague Jake wrote a blog about using Deephaven and Python's websockets to subscribe to Coinbase's Websocket Feed. Here, we revisit Jake's example to bring it up to speed with Deephaven's latest API. Jake's query is an excellent use case for the recently released Table Publisher.

A Table Publisher allows users to programmatically add data to a blink table. It's a perfect candidate for Python's asyncio and websockets libraries to ingest crypto data in real-time from Coinbase.

With websockets, asyncio, and Deephaven's Table Publisher, you can create a reproducible end-to-end solution to ingest and analyze real-time crypto data from Coinbase's websocket feed.

Python and websockets

The websockets package is required to run the code in this blog. It's easy to set up and use with Deephaven. For this guide, we'll install it using os and pip.

Deephaven's Table Publisher is a new, preferable option to the DynamicTableWriter for writing data to tables in real time. Where a DynamicTableWriter writes one row at a time to an append-only table, a Table Publisher adds entire tables to a blink table, which only keeps rows from the current update cycle. Rows from previous update cycles disappear from the UI and memory as soon as new data enters the table.

By itself, a blink table is a powerful tool to ingest data from external sources in real time without having to worry about memory constraints. The tradeoff is that a blink table doesn't store any history. However, it's easy to convert a blink table to either a ring table or an append-only table. Both keep data history, which is critical for some real-world applications.

Coinbase Websockets

Like Jake's blog, the subscribe-and-publish model is at the heart of the code used to ingest data from Coinbase's websocket feed. Below is what it looks like to make a request to the feed with Python.

You could run this in a while True to ingest and print the data forever. We don't recommend doing so, but to illustrate:

Putting it all together

The entire application consists of three scripts, which should be run in the order given in this blog.

This first script sets up the methods to ingest data from the Coinbase websocket feed using asynchronous I/O.

This second script sets up the Table Publisher and its supported methods, which convert ingested data, enable clean shutdown, and create the table to which the data will be published.

The third script creates an asynchronous event loop and runs the code to create the blink table. Downstream operations can convert it to table types that keep data history.

img

Watch and see

Reach out

Have questions or comments? Reach out on Slack!