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