---
title: "Connecting to SurrealDB"
description: "Create a SurrealHTTPClient or SurrealWebSocketClient, connect, select a namespace and database, configure reconnection and timeouts, and close the connection in the Swift SDK."
canonical_url: "https://surrealdb.com/docs/reference/swift/concepts/connecting-to-surrealdb"
sdk: "Swift SDK"
surrealdb_version: "4.x"
last_modified: "2026-11-24T05:29:47Z"
---

> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# Connecting to SurrealDB

Create a SurrealHTTPClient or SurrealWebSocketClient, connect, select a namespace and database, configure reconnection and timeouts, and close the connection 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, and the WebSocket client additionally supports [live queries](/docs/reference/swift/concepts/live-queries.md). The methods below are available on both clients.

<table>
	<thead>
		<tr>
			<th scope="col">Method</th>
			<th scope="col">Description</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td scope="row" data-label="Method"><a href="#connect"> <code> client.connect() </code></a></td>
			<td scope="row" data-label="Description">Connects the client to the endpoint it was configured with</td>
		</tr>
		<tr>
			<td scope="row" data-label="Method"><a href="#use"> <code> client.use(namespace, database) </code></a></td>
			<td scope="row" data-label="Description">Switches to a specific namespace and database</td>
		</tr>
	</tbody>
</table>

## 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. 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() } }
```

## `.connect()` {#connect}

Connects the client to the endpoint it was configured with.

```swift title="Method Syntax"
try await client.connect()
```

### Example usage

```swift
let client = try SurrealHTTPClient(endpoint: "http://localhost:8000")
try await client.connect()
defer { Task { await client.close() } }
```

## `.use()` {#use}

Switches the connection to a specific namespace and database. Once connected, select the namespace and database your queries should run against.

```swift title="Method Syntax"
try await client.use(namespace: ns, database: db)
```

### Arguments

| Argument | Description |
| --- | --- |
| `namespace` (required) | The namespace to switch to. |
| `database` (required) | The database to switch to. |

### Example usage

```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()
```
