# Connecting to SurrealDB

Learn how to connect to SurrealDB over HTTP or WebSocket and configure the client in the Swift SDK.

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](/docs/reference/swift/concepts/live-queries.md).

## HTTP client

`SurrealHTTPClient` connects over HTTP and is the simplest choice for request/response workloads.

```swift
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](/docs/reference/swift/concepts/live-queries.md). It can also automatically reconnect when a connection is dropped.

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

```swift
try await client.use(namespace: "myapp", database: "mydb")
```

## Configuration options

### `SurrealClientOptions`

Common options shared by both the HTTP and WebSocket clients:

```swift
SurrealClientOptions(
    requestTimeout: 20,
    pingInterval: 30
)
```

### `SurrealWebSocketOptions`

Reconnection behaviour for the WebSocket client:

```swift
SurrealWebSocketOptions(
    reconnectEnabled: true,
    maxReconnectAttempts: 8,
    reconnectBaseDelay: 0.5
)
```

## Closing the connection

Always close the client when you are done to release the underlying connection:

```swift
await client.close()
```
