Terra Docs
Dashboard
  • Docs
  • API Reference
  • Changelog
  • Get started ⚡️ Choose your integration
  • Health & Fitness API
    • Overview
    • Set up your Integrations
    • Connect a User
    • Receive data updates
    • Request Historical data
    • Write data
    • Debugging FAQ
    • Mobile-Only Sources: Apple, Samsung, Google Fit
      • iOS (Swift)
      • Android (Kotlin)
      • React Native
      • Flutter
  • Streaming API
    • Getting Started
    • Wearable -> Your app
      • iOS (Swift)
      • Android
    • Your app -> Terra
      • iOS (Swift)
      • Android
    • Terra -> Your Backend
  • Teams API
  • Biomarkers API - Upcoming
Powered by GitBook
On this page

Was this helpful?

  1. Streaming API
  2. Your app -> Terra

iOS (Swift)

PreviousYour app -> TerraNextAndroid

Last updated 7 months ago

Was this helpful?

Once you've started receiving data from a device into your app, you can start sending this data to Terra's websocket server (the broker).

This will later allow you to receive it on your backend, and process it as per your requirements.

Prerequisite

Before following the steps below, make sure you've followed the guide to

Setting up a connection

You'll have previously set up a stream between a wearable device and your app . In order to to send this data back to the broker, you'll simply use a variant of the startRealTime function

import TerraRT

// Example function to start real-time data streaming, stop it, and disconnect
func manageRealtimeData() {
    let connectionType: TerraRTiOS.Connections = .BLE  // Replace with your connection type (e.g., BLE)
    let dataTypes: Set<TerraRTiOS.DataTypes> = [.heartRate, .steps]  // Replace with desired data types to stream
    let token = "yourAuthToken"  // Replace with your actual token, fetched from your backend

    // Start real-time data streaming
    terraRT.startRealtime(type: connectionType, dataType: dataTypes, token: token, callback: { update in
        handleUpdate(update: update)
    }, connectionCallback: { success in
        if success {
            print("Real-time connection successfully established")
        } else {
            print("Failed to establish real-time connection")
        }
    })
    
    // Stop the real-time streaming after some action or condition
    // Example: stopping after a delay (5 seconds here, for demonstration)
    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        terraRT.stopRealtime(type: connectionType)
        print("Real-time streaming stopped.")
    }

    // Optionally, disconnect after stopping the real-time data stream
    DispatchQueue.main.asyncAfter(deadline: .now() + 7) {
        terraRT.disconnect(type: connectionType)
        print("Disconnected from connection.")
    }
}

// Function to handle updates from the real-time data stream
func handleUpdate(update: TerraRTiOS.Update) {
    // Process the real-time data update here
    print("Received update at timestamp: \(update.ts)")
    print("Update type: \(update.type)")
    if let value = update.val {
        print("Value: \(value)")
    }
    if let dataArray = update.d {
        print("Array of data values: \(dataArray)")
    }
}

// Call the function to start the real-time data management process
manageRealtimeData()

Simply passing in a token from the following endpoint will allow you to stream the same data you were reading previously within your app, and have it sent to the broker

stream data from a wearable device to your app

Stream - Generate user token

post

Endpoint for generation of a token for a user (producer) connection

Authorizations
Query parameters
idstringOptional

The ID of the user to generate a token for

Header parameters
dev-idstringRequired

your developer ID

x-api-keystringRequired

your API key

Responses
200
Successful response
application/json
403
Forbidden
text/plain
post
POST /auth/user HTTP/1.1
Host: ws.tryterra.co
x-api-key: text
dev-id: text
Accept: */*
{
  "token": "OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eE"
}
  • Setting up a connection
  • POSTStream - Generate user token
using the startRealTime function