AccountDataStreamItem
public final class AccountDataStreamItem : Sendable
Streams account data entry updates from the Horizon API using Server-Sent Events (SSE) for real-time updates.
This stream provides live updates for a specific data entry (key-value pair) on an account. Each update contains the current base64-encoded value for the specified key as it changes on the Stellar network.
Accounts can store arbitrary key-value pairs (up to 64 bytes per value) using the ManageDataOperation. This stream allows you to monitor changes to a specific key in real-time.
Example usage:
let sdk = StellarSDK()
let dataStream = sdk.accounts.streamAccountData(
accountId: "GACCOUNT...",
key: "user_settings"
)
dataStream.onReceive { response in
switch response {
case .open:
print("Stream connection established")
case .response(id: let id, data: let dataEntry):
// Decode base64 value
if let decoded = Data(base64Encoded: dataEntry.value) {
let value = String(data: decoded, encoding: .utf8) ?? ""
print("Data updated - Value: \(value)")
}
case .error(let error):
print("Stream error: \(error)")
}
}
// Close stream when done
dataStream.closeStream()
See also:
- Stellar developer docs
- DataForAccountResponse for the data entry structure
- ManageDataOperation for setting account data
-
Creates a new account data stream for the specified Horizon API endpoint.
Declaration
Swift
public init(requestUrl: String)Parameters
requestUrlThe complete Horizon API URL for streaming the account data entry
-
Establishes the SSE connection and delivers data entry responses as they arrive from Horizon.
The response closure is called multiple times:
- Once with .open when the connection is established
- Each time with .response when the data entry is updated
With .error if any error occurs during streaming
Declaration
Swift
public func onReceive(response: @escaping StreamResponseEnum<DataForAccountResponse>.ResponseClosure)Parameters
responseClosure called with stream events. Called on a background thread.
-
Closes the event stream and releases resources.
Call this method when you no longer need to receive updates for the account data entry. After closing, the stream cannot be reopened - create a new AccountDataStreamItem instead.
Declaration
Swift
public func closeStream()
View on GitHub
Install in Dash