# iOS (Swift)

Once you've started receiving data from a [device](https://app.gitbook.com/s/eJJpVMsUARUJq9lYmL6t/streaming-api/core-concepts#device) into your app, you can start sending this data to Terra's websocket server (the [broker](https://app.gitbook.com/s/eJJpVMsUARUJq9lYmL6t/streaming-api/core-concepts#broker)).

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

{% hint style="info" %}

#### Prerequisite

Before following the steps below, make sure you've followed the guide to [stream data from a wearable device to your app](https://docs.tryterra.co/streaming-api/connect-wearable-to-sdk/ios-swift)
{% endhint %}

## Setting up a connection

You'll have previously set up a local stream between a wearable [device](https://app.gitbook.com/s/eJJpVMsUARUJq9lYmL6t/streaming-api/core-concepts#device) and your app [using the startRealtime function](https://docs.tryterra.co/connect-wearable-to-sdk/ios-swift#streaming-real-time-data). In order to send this data to the [broker](https://app.gitbook.com/s/eJJpVMsUARUJq9lYmL6t/streaming-api/core-concepts#broker), use the `startRealtime` overload that accepts a `token`:

```swift
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.

{% openapi src="<https://raw.githubusercontent.com/tryterra/openapi/refs/heads/master/rt.yml>" path="/auth/user" method="post" %}
<https://raw.githubusercontent.com/tryterra/openapi/refs/heads/master/rt.yml>
{% endopenapi %}
