In Ditto, synchronization happens over a variety of network transports — including local peer-to-peer connections (Bluetooth, LAN, AWDL, WI-FI Aware, and Wi-Fi), as well as cloud-based sync via the Ditto Server. While Ditto handles these connections automatically by default, you may want more control over which transports are used, how they’re configured, or when they’re enabled. This guide walks you through how to customize these transport settings to suit your app’s specific connectivity and sync requirements.
Public Preview - This version is in public preview and subject to changes. For production use, please use SDK v4.
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.
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.
Copy
Ask AI
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)}
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.
Copy
Ask AI
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 queriestry await ditto.store.execute("ALTER SYSTEM SET DQL_STRICT_MODE = false")do { try ditto.startSync()} catch { print(error.localizedDescription)}
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 (IP) and port:
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.
Copy
Ask AI
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)}
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.
Copy
Ask AI
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 transportConfig.connect.tcpServers.insert("135.1.5.5:12345") transportConfig.connect.tcpServers.insert("185.1.5.5:12345")}do { try ditto.startSync()} catch (let err) { print(err.localizedDescription)}
ditto.delegate = selftry! 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") } }}