The Swift SDK provides two transports: an HTTP client and a WebSocket client. Both share the same querying and CRUD API; the WebSocket client additionally supports live queries.
HTTP client
SurrealHTTPClient connects over HTTP and is the simplest choice for request/response workloads.
let client = try SurrealHTTPClient(endpoint: "http://localhost:8000")
try await client.connect()
defer { Task { await client.close() } }WebSocket client
SurrealWebSocketClient connects over WebSocket and is required for live queries. It can also automatically reconnect when a connection is dropped.
let client = try SurrealWebSocketClient(
endpoint: "ws://localhost:8000",
websocketOptions: SurrealWebSocketOptions(
reconnectEnabled: true,
maxReconnectAttempts: 8,
reconnectBaseDelay: 0.5
)
)
try await client.connect()
defer { Task { await client.close() } }Selecting a namespace and database
Once connected, select the namespace and database your queries should run against:
try await client.use(namespace: "myapp", database: "mydb")Configuration options
SurrealClientOptions
Common options shared by both the HTTP and WebSocket clients:
SurrealClientOptions(
requestTimeout: 20,
pingInterval: 30
) SurrealWebSocketOptions
Reconnection behaviour for the WebSocket client:
SurrealWebSocketOptions(
reconnectEnabled: true,
maxReconnectAttempts: 8,
reconnectBaseDelay: 0.5
)Closing the connection
Always close the client when you are done to release the underlying connection:
await client.close()