Coinbase Websockets and Asyncpg for Timescaledb insertion

I am interested in getting Coinbase Pro data via websockets which is an async library for streaming Websocket data using Python

I was wondering if it is possible to use Asyncpg when receiving a message from the Websockets and push it to the timescaledb?

I am new to timescaledb and my googling hasn’t revealed anything useful.

I would appreciate if anyone could help or provide with some initial boiler code?

Cheers

1 Like

@fahadshery,

Welcome to the TimescaleDB Forum and community! :tada:

Currently Coinbase Pro isn’t on our roadmap because we aren’t able to reuse the data for tutorials based on their licensing. Instead, we currently use Twelve Data to source our stock and crypto data through their websocket API and we will be providing an example of that process soon as a tutorial on our documentation website.

That said, TimescaleDB is PostgreSQL. Any tutorial you find about using Asyncpg to save data from a websocket into Postgres will work with TimescaleDB. The only difference is that your time-series tables should be created as Hypertables. Assuming you’re using the Coinbase API to pull in trade/tick data, we have a tutorial that was just released which will provide a great foundation for some schema ideas and how to use Continuous Aggregates and compression.

Please have a look at Store financial tick data in TimescaleDB using the OHLCV (candlestick) format to get you started.

Also, if you come up with a sample and want to start a PR to our docs (or post a link to a Github example here), please feel free to contribute that back too!

Hi Ryan,

Thank you for your message. Yes, I have no problem working with any Websocket api’s.
The examples in your online tutorials are typically for one time data ingestion. For example: downloading data in csv and then uploading in a hyper table.

I am comfortable with creating hyper tables and pushing the data using psycopg2 etc. but I believe we can further performance gain if we use asyncpg instead of the normal ways of creating/destroying connections when connecting to postgres.

Have a look at this: https://magic.io/blog/asyncpg-1m-rows-from-postgres-to-python/

I am trying websockets (instead of Websocket) along with psycopg2 to ingest Websocket data in postgres (timescaledb) but having some issues.

This was the primary reason of reaching out to you experts to see if this is something you guys have implemented already.

One more example is twitter Websocket data for influencer or keyword based tweets and using it for predictions.

Hope this clarifies the issue.

Cheers,

2 Likes

I’ve used bare aiohttp as a server but not the websockets module. That said, migrating a streaming aiohttp server from psycopg2 to asyncpg let me cut my kubernetes pod count by around 75% and reduce frontend latency. The binary copy_records_to_table is very good, especially with large batch sizes.

2 Likes

Looking forward to see the Twelve Data websocket tutorial :sunglasses:

1 Like

that’s exactly what I want to use copy_records_to_table from Asyncpg.

It does and there’s some great stuff to think about there.

For now, we’re going to start with the script we have which uses psycopg2 so that we can make the resource available ASAP for users to start getting data.

However, asyncpg looks really interesting and we’ve already discussed how we can provide some good examples and (maybe) even do some comparisions. We’ll update this thread with any relevant content that we publish over the next few months!

2 Likes