Realtime Data Streaming - Terra API
Terra has been near-real-time since the start. We send a complete payload about an activity or sleep session of a user in one Webhook URL. However, Terra's vision goes beyond this simplistic model. We want to provide real-time data and stream it to our developers as the user does their workout. Today, we make a huge step towards this goal with our new products: Websocket API along with Bluetooth Low Energy (BLE) connections to stream real-time data from wearables to you!
The BLE Connection
BLE connections are done through our Software Development Kits (SDKs). You can seamlessly integrate our SDKs into your app and allow your users to connect to any BLE devices they wish (we support both iOS and Android devices with our SDKs).
We even provide the widget for you and automatic connection of the user to our Websocket API so the only thing you need to do is control when to start or stop streaming.
Streaming Heart Rate Data
After you have established a connection to a BLE device using our Widget above, you can immediately start streaming their data to our Websocket API! As an example, we will try streaming Heart Rate data.
In the widget screen above, TICKR FIT 75B1 is a Heart Rate monitor strap from Wahoo. We will stream the heart rate values from this strap to you through our Websocket API.
Let's say you have pressed "Connect" and it was successful. You can then simply run a function to start streaming (more info on the code in our repository (Android's SDK is used here as an example))
terraRT.startRealtime(type: Connections.BLE, dataTypes: setOf(DataTypes.HEART_RATE))
This function does:
- Establish that the device allows for "HEART_RATE" data streaming
- Enables the "HEART_RATE" streaming service from the device
- Opens and starts streaming the heart rate values to our Websocket API.
That's it on the user's end.
As a developer, you can then connect to our Websocket API to receive this user's data. Let's connect and see what we get:
22704: ↓ {"op":5,"d":{"ts":"2022-06-14T13:24:21.660Z","val":77.0},"uid":"7af41b43-ee18-4222-b4cf-8ab218cf7b6d","seq":14115,"t":"HEART_RATE"} 22704: ↓ {"op":5,"d":{"ts":"2022-06-14T13:24:22.694Z","val":76.0},"uid":"7af41b43-ee18-4222-b4cf-8ab218cf7b6d","seq":14116,"t":"HEART_RATE"} 22704: ↓ {"op":5,"d":{"ts":"2022-06-14T13:24:23.692Z","val":76.0},"uid":"7af41b43-ee18-4222-b4cf-8ab218cf7b6d","seq":14116,"t":"HEART_RATE"} 23510: ↓ {"op":5,"d":{"ts":"2022-06-14T13:24:24.674Z","val":77.0},"uid":"7af41b43-ee18-4222-b4cf-8ab218cf7b6d","seq":14117,"t":"HEART_RATE"} 26685: ↓ {"op":5,"d":{"ts":"2022-06-14T13:24:25.670Z","val":77.0},"uid":"7af41b43-ee18-4222-b4cf-8ab218cf7b6d","seq":14118,"t":"HEART_RATE"}
Incredible! Data from a user connecting their BLE device to your app straight to your backend.
Seems too easy? It's because we made it that way!
Time to stream
The example above shows a simple yet powerful tool that many developers can benefit from.
For example, you can:
- Build a treadmill that can stream a user's heart rate from their chest strap and display it on the screen
- Create a virtual reality fitness game where you can stream the player's heart rate, steps, calories burnt, or distance traveled from outside sources
- Dynamically adapt your workout in an app depending on the user's live heart rate data
- Recommend music based on the user's current heart rate
If you have any other ideas in mind, go and build it! We did all the heavy lifting in getting the data to you. Now it's time for you to show us what your vision is.