You can read more about the various transports that Ditto uses in Mesh Networking.
Although Ditto automatically attempts to connect to other instances on the Local Area Network (LAN), Bluetooth Low Energy (LE), and Apple Wireless Direct Link (AWDL), supplying a custom instance of the DittoTransportConfig does not enable this feature by default. Rather, you manually enable peer-to-peer connections using EnableAllPeerToPeer().This is only true if you provide a custom instance of the DittoTransportConfig object. If you do not provide a custom instance, Ditto will automatically enable all peer-to-peer transports by default.The best way to ensure that you are using the correct transport configuration is to use the updateTransportConfig API to update the DittoTransportConfig object. This will ensure that you are using the correct transport configuration and that you are not missing any transports.

Enabling and Disabling Transports

When a new Ditto instance is created, a default transport configuration is supplied that enables all peer-to-peer transports by default. You can control which transports are enabled or disabled by updating the DittoTransportConfig object.
Changing the transport configuration via the updateTransportConfig API after sync has been started will instantly apply the changes without the need to manually stop and start sync.
ditto.updateTransportConfig { transportConfig in
  //enable/disable each transport separately

  //BluetoothLe
  transportConfig.peerToPeer.bluetoothLE.isEnabled = true
  //Local Area Network
  transportConfig.peerToPeer.lan.isEnabled = true
  //Awdl
  transportConfig.peerToPeer.awdl.isEnabled = true
}

do {
  try ditto.startSync()
} catch (let err) {
  print(err.localizedDescription)
}

Syncing with Ditto Server / WebSocket Connections

To sync with the Ditto Server or to initialize a WebSocket connection, you need to add a websocketURL to the DittoTransportConfig object. You can do this by calling updateTransportConfig and adding the websocketURL to the connect.websocketURLs array.
Using the updateTransportConfig API to remove all webSocketURLs from the connect object will disable sync with the Ditto Server instantly without needing to call stop and start sync on the Ditto instance.
ditto = Ditto(
  identity: DittoIdentity.onlinePlayground(
    appID: "REPLACE_ME_WITH_YOUR_APP_ID",
    token: "REPLACE_ME_WITH_YOUR_PLAYGROUND_TOKEN",
    enableDittoCloudSync: false, // This is required to be set to false to use the correct URLs
    customAuthURL: URL(string: "REPLACE_ME_WITH_YOUR_AUTH_URL")
  )
)

ditto.updateTransportConfig { transportConfig in
  // Set the Ditto Websocket URL
  transportConfig.connect.webSocketURLs.insert("wss://REPLACE_ME_WITH_YOUR_WEBSOCKET_URL")
}

// Disable DQL strict mode so that collection definitions are not required in DQL queries
try await ditto.store.execute("ALTER SYSTEM SET DQL_STRICT_MODE = false")

do {
  try ditto.startSync()
} catch {
  print(error.localizedDescription)
}

Configuring Additional Settings

If you need additional connection configurations for the current Ditto instance, configure it to listen for connections on a specific port and to connect to remote instances using a host and port:

Connecting to Remote Small Peers

Supported in SDK version 4.12.0 and later. On previous SDK versions, use transportConfig.connect.tcpServers for equivalent behavior.
The configuration for connecting to remote small peers uses DQL queries rather than the updateTransportConfig API. This system configuration can be updated at any time, though changes will only take effect while sync is active. Suppose you have a device in your mesh that acts as a connection hub, and you want every other device to connect to the hub.
  1. Define the TCP listening port (e.g. 12345) as described in Listening for Connections.
  2. Retrieve the IP address of the hub device, or assign it a static IP. In this example, the IP address is 10.0.0.143.
  3. Set TRANSPORTS_DISCOVERED_PEERS on all the other devices in the mesh to make them connect to the hub. See code examples below:

await ditto.store.execute("""
  ALTER SYSTEM SET TRANSPORTS_DISCOVERED_PEERS =
  [{'address': 'tcp://10.0.0.143:12345', 'type': 'force'}]
""");
The address field supports domain names, IPv4 addresses, and IPv6 addresses. tcp is the only supported scheme. The following are all valid addresses:
  • tcp://192.168.1.86:12345
  • tcp://[2001:db8::1:0]:12345
  • tcp://mydevice.local:12345
The type field supports two options:
  • candidate (default): Ditto will use this peer as a connection candidate when forming an optimal mesh. If this peer is removed from the list, the connection may persist.
  • force: Ditto will always attempt to connect to this peer. If this peer is removed from the list, the connection will immediately be dropped.

Listening for Connections

For some use cases, you can configure a Ditto instance to accept incoming TCP connections from remote peers at a specific IP address and port. This is different from the automatic peer-to-peer discovery that happens by default. Use this when you want your device to act as a connection hub that other devices can connect to directly, rather than relying solely on automatic discovery mechanisms like mDNS or Bluetooth.
ditto.updateTransportConfig { transportConfig in
  // Listen for incoming connections on port 4000

  // By default Tcp Enabled is false, be sure to set it to true.
  transportConfig.listen.tcp.isEnabled = true
  // if you want to listen on localhost, most likely you will use 0.0.0.0
  // do not use "localhost" as a string
  transportConfig.listen.tcp.interfaceIP = "0.0.0.0"
  transportConfig.listen.tcp.port = 4000
}

do {
  try ditto.startSync()
} catch (let err) {
  print(err.localizedDescription)
}

Combining Multiple Transports

A reminder when a new Ditto instance is created, a default transport configuration is supplied that enables all peer-to-peer transports by default. You can combine multiple transports to suit your needs. For example, you can listen for incoming connections on a specific port and connect to remote devices.

ditto.updateTransportConfig { transportConfig in
  // 1. Listen for incoming connections on port 4000
  transportConfig.listen.tcp.isEnabled = true
  transportConfig.listen.tcp.interfaceIP = "0.0.0.0"
  transportConfig.listen.tcp.port = 4000
}

// 2. Connect explicitly to remote devices
await ditto.store.execute("""
  ALTER SYSTEM SET TRANSPORTS_DISCOVERED_PEERS =
  [{'address': 'tcp://135.1.5.5:12345', 'type': 'force'},
  {'address': 'tcp://185.1.5.5:12345', 'type': 'force'}]
""");

do {
  try ditto.startSync()
} catch (let err) {
  print(err.localizedDescription)
}

Replacing mDNS Discovery

Supported in SDK version 4.12.0 and later.
In rare cases where mDNS discovery may not be suitable, Ditto gives you full control over LAN discovery. On each device:
  1. Disable peer-to-peer LAN in the TransportConfig.
  2. Configure the TCP server to listen on a defined port. This port does not have to be the same for each peer, but you do have to define it for each peer.
  3. Read the discovery_hint using the following DQL query:
  • SELECT * FROM system:transports_info WHERE _id = 'discovery_hint'
  1. Get the device’s IP address on the local network.
  2. Send the device’s IP address, TCP listening port, and discovery_hint to every other peer in the mesh. (This information must be sent on channels outside Ditto because we assume that all peers start disconnected from each other.)
  3. Update TRANSPORTS_DISCOVERED_PEERS with the full list of discovered peers as the device receives discovery information from other devices.

// Listen for connections on port 12345
ditto.updateTransportConfig { transportConfig in
  transportConfig.listen.tcp.isEnabled = true
  transportConfig.listen.tcp.interfaceIP = "0.0.0.0"
  transportConfig.listen.tcp.port = 12345
}

// Get the device's IP address (platform-specific)
// ...

// Start sync
do {
  try ditto.startSync()
} catch (let err) {
  print(err.localizedDescription)
}

// Get the device's discovery hint (this may change every time startSync() is called)
if let discoveryHint = await getDiscoveryHint(ditto) {
  // Discovery hint available here
}

// Broadcast local IP, port (12345), and discovery hint to all other devices
// ...

// As we receive broadcasts from other devices, update TRANSPORTS_DISCOVERED_PEERS with the full list of discovered peers
await ditto.store.execute("""
  ALTER SYSTEM SET TRANSPORTS_DISCOVERED_PEERS =
  [{'address': 'tcp://10.0.0.143:12345', 'discovery_hint': 'Q2CGGdmF-A', 'type': 'candidate'},
  {'address': 'tcp://10.0.0.154:12345', 'discovery_hint': 'Q2CG6y5vGw', 'type': 'candidate'}]
""");

// ...

// Function definition for completeness
func getDiscoveryHint(ditto: Ditto) async -> String? {
  let discoveryHint = try? await ditto.store
    .execute(query: "SELECT * FROM system:transports_info WHERE _id = 'discovery_hint'")
  return discoveryHint?.items.first?.value["value"] as? String
}
Notes about discovery_hint:
  • It is an optional field in TRANSPORTS_DISCOVERED_PEERS.
  • It is an opaque string that uniquely identifies each peer in the mesh. It is used to make more intelligent decisions about which peers to connect to.
  • It may change for a peer as often as startSync() is called.
  • It does not contain information that could be used to track a device long-term, and it is safe to advertise in the clear.

Monitoring Conditions

// Setting up inside a ViewController
let ditto = Ditto(identity: DittoIdentity.onlinePlayground(appID: "REPLACE_ME_WITH_YOUR_APP_ID", token: "REPLACE_ME_WITH_YOUR_PLAYGROUND_TOKEN"))
ditto.delegate = self
try! ditto.startSync()

// Now you can observe real time changes to the transport conditions:
extension ViewController: DittoDelegate {
   func transportConditionDidChange(transportID: Int64, condition: TransportCondition) {
       if condition == .BleDisabled {
           print("BLE disabled")
       } else if condition == .NoBleCentralPermission {
           print("Permission missing for BLE")
       } else if condition == .NoBlePeripheralPermission {
           print("Permission missing for BLE")
       }
   }
}