Quick Start

For normal WebSockets stream with JSON data, first you need to create a model structure, which represents the expected result:

struct Coin: Codable {
var ticker: String
var price: Double
}
💡

If you want to process the data manually or have a WebSocket communicating in a different way, you can create your own data processor. Check here for more examples.

Then you can import Gravity and start using it inside any view components:

import SwiftUI
import Gravity
struct CoinPrice: View {
@GravityStream(url: "wss://example.org/api/endpoint", model: Coin) var stream
var body: some View {
if let coin = stream.data {
Text("The price of \(coin.ticker) is \(coin.price)")
}
}
}

Normally, there're 3 possible states of a request: "loading", "ready", or "error". You can use the value of data and error to determine the current state of the request, and return the corresponding UI.