Build a Real-Time Crypto Ticker With Go and InfluxDB 2 | by Bertrand Quenin | Sep, 2022

Using Coinbase API

Photo by Jeremy Bezanger on Unsplash

We’ll see how to implement a crypto ticker in Go using live data from the Coinbase Pro API and Influx DB 2 to store and plot data. All the code for this story is available in my GitHub repository.

  • The crypto ticker connects to the Coinbase Pro API using a WebSocket and subscribes to cryptocurrency’s price information for a given pair, e.g. BTC-USD.
  • The Coinbase server starts streaming the price data to the ticker which inserts it into InfluxDB as a measurement.
  • Finally, we connect to the database UI on http://localhost:8086/

The ticker’s role is extremely simple:

  • Subscribe to the Coinbase API using a WebSocket. Thankfully for us, the Coinbase WebSocket API allows anonymous access, it’s not required to create an API key to access tick data:
  • Each time a tick is received, write it into InfluxDB:

InfluxDB is a time series database perfectly suited for our use case. Since we are building a real-time ticker, we receive events from Coinbase every time there’s a transaction, multiple times a second. This time resolution is very precise, allowing for all sorts of real-time computation.

However, after about an hour, our data is obsolete, so we configure our bucket retention accordingly. InfluxDB will simply discard outdated data for us.

Running InfluxDB as a Container

To configure the InfluxDB container, we must run it in automated setup mode. The InfluxDB image contains some functionality to bootstrap the system automatically. This is enabled by setting the DOCKER_INFLUXDB_INIT_MODE environment variable to the value setup when running the container:

InfluxDB 2.0 Automated Setup
  • DOCKER_INFLUXDB_INIT_USERNAME: The username to set for the system’s initial super-user (Required).
  • DOCKER_INFLUXDB_INIT_PASSWORD: The password to set for the system’s initial super-user (Required).
  • DOCKER_INFLUXDB_INIT_ORG: The name to set for the system’s initial organization (Required).
  • DOCKER_INFLUXDB_INIT_BUCKET: The name to set for the system’s initial bucket (Required).
  • DOCKER_INFLUXDB_INIT_RETENTION: The duration of the system’s initial bucket should retain data. One hour for our use case.
  • DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: The authentication token to associate with the system’s initial super-user. If not set, a token will be auto-generated by the system.

I’m using direnv to provide the environment variable substitutions to the Docker Compose file.

We are also exposing the InfluxDB port to the host so that we can connect to the UI.

Finally, we provide an initialization script in the /docker-entrypoint-initdb.d folder to import a predefined dashboard. We are mounting the ./influxdb/initfolder:

└── init
└── templates
└── tickers.yaml

Now that we have configured everything, we can just start the stack using:

make start

You can now connect to the UI on http://localhost:8086/ and check the dashboard:

Crypto Ticker using InfluxDB UI

Tada! Now you have your own real-time crypto ticker up and running. You can now learn more about the Flux language to build your own metrics!

News Credit

%d bloggers like this: