iOS (Swift)
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 stream data from a wearable device to your app
Setting up a connection
You'll have previously set up a local stream between a wearable device and your app using the startRealtime function. In order to send this data to the broker, use the startRealtime overload that accepts a token:
import TerraRTiOS
// Assumes terraRT is already initialized and a device is connected
// (see the "Connect Wearable to SDK" guide)
let token = "yourStreamingToken" // Generated from your backend (see below)
let dataTypes: Set<TerraRTiOS.DataTypes> = [.HEART_RATE, .STEPS]
// Stream to BOTH your app AND Terra's websocket server
terraRT.startRealtime(
type: .BLE,
dataType: dataTypes,
token: token,
callback: { update in
print("\(update.type ?? ""): \(update.val ?? 0) at \(update.ts ?? "")")
},
connectionCallback: { connected in
print("Websocket connected: \(connected)")
}
)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.
Generates a single-use token for a user (producer) connection. The id
identifies the user the token is scoped to — a v6 connection id or a v5
compatibility user UUID — and must belong to the authenticating
developer.
Your API key for authentication
Your developer ID for authentication and tracking
The id of the user to generate a token for.
Token generated.
Single-use token to present in the IDENTIFY frame.
OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eEMissing id parameter, or missing/malformed authentication headers.
Invalid credentials, or the user does not belong to this developer.
Token generation failed.
POST /auth/user?id=text HTTP/1.1
Host: ws.tryterra.co
x-api-key: YOUR_API_KEY
dev-id: YOUR_API_KEY
Accept: */*
{
"token": "OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eE"
}Last updated
Was this helpful?