Customizing Transports Configurations
This article provides instructions on explicitly configuring your Ditto instance to listen for connections on a specific port and to connect to remote instances using a host (IP) and port.
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 DittoTransportConfig does not enable this feature by default.
Rather, you manually enable peer-to-peer connections using EnableAllPeerToPeer(). (See Enabling and Disabling Transports)
Enabling and Disabling Transports
final config = TransportConfig()
..setAllPeerToPeerEnabled(true)
..peerToPeer.bluetoothLE.isEnabled = true
..peerToPeer.lan.isEnabled = true
..peerToPeer.awdl.isEnabled = true;
await ditto.setTransportConfig(config);
await ditto.startSync();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 (IP) and port:
Before calling startSync(), create a DittoTransportConfig object with the desired settings.
Using the ditto.SetTransportConfig API method, set the created object on your Ditto instance.
Call the startSync method on your Ditto instance to initiate sync with the configured settings.
Syncing with Big Peer
final identity = await OnlinePlaygroundIdentity.create(
appID: 'REPLACE_ME_WITH_YOUR_APP_ID',
token: 'REPLACE_ME_WITH_YOUR_PLAYGROUND_TOKEN',
// Set to false to disable syncing with the cloud
enableDittoCloudSync: true,
);
final ditto = await Ditto.open(
identity: identity,
persistenceDirectory: persistenceDirectory,
);
await ditto.startSync();Connecting to Remote Small Peers
final config = TransportConfig()
..connect.websocketURLs.push('wss://135.1.5.5:12345')
..connect.websocketURLs.push('wss://185.1.5.5:12345');
await ditto.setTransportConfig(config);
await ditto.startSync();Initializing WebSocket Connections
Ensure WebSocket connections are discoverable across peers within your local network:
final config = TransportConfig()
..connect.websocketURLs.push('ws://127.0.0.1');
await ditto.setTransportConfig(config);
await ditto.startSync();Listening for Connections
final config = TransportConfig()
..listen.tcp.isEnabled = true
..listen.tcp.interfaceIP = '0.0.0.0'
..listen.tcp.port = 4000;
await ditto.setTransportConfig(config);
await ditto.startSync();Combining Multiple Transports
final config = TransportConfig()
..setAllPeerToPeerEnabled(true)
..listen.tcp.isEnabled = true
..listen.tcp.interfaceIP = '0.0.0.0'
..listen.tcp.port = 4000;
// 3. Connect explicitly to remote devices
await ditto.setTransportConfig(config);
await ditto.startSync();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")
}
}