Configuring Transports
This article provides instructions on how to explicitly configure 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.
You must manually enable peer-to-peer connections using EnableAllPeerToPeer(). For instructions, see Enabling and Disabling Transports.
Configuring Additional Settings
In some cases, you may need to set up additional connection configurations for the current Ditto instance. This section provides instructions on how to explicitly configure your Ditto instance to listen for connections on a specific port and to connect to remote instances using a host (IP) and port.
To configure additional connection settings for the current Ditto instance, before calling startSync(), construct a DittoTransportConfig value and set it using ditto.SetTransportConfig.
Enabling and Disabling Transports
// Create a new DittoTransportConfig()
var config = DittoTransportConfig()
//Enable all peer to peer transports
config.enableAllPeerToPeer()
//Or enable/disable each transport separately
//BluetoothLe
config.peerToPeer.bluetoothLE.isEnabled = true
//Local Area Network
config.peerToPeer.lan.isEnabled = true
//Awdl
config.peerToPeer.awdl.isEnabled = true
ditto.transportConfig = config
do {
try ditto.startSync()
} catch (let err) {
print(err.localizedDescription)
}Syncing with Big Peer
let ditto = Ditto(identity: .onlinePlayground(
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
))
do {
try ditto.startSync()
} catch (let err) {
print(err.localizedDescription)
}Observing Peers
ditto.deviceName = "Susan B.";
try {
ditto.startSync();
} catch(DittoError e) {
// handle error
}
ditto.getPresence().observe(peers -> {
// render peers
});"status == 'open' && edited_by == 'deviceC'"Connecting to Remote Small Peers
auto config = ditto::TransportConfig();
// Connect explicitly to remote devices
config.connect.tcp_servers.insert("135.1.5.5:12345");
config.connect.tcp_servers.insert("185.1.5.5:12345");
// set the transport config
ditto.set_transport_config(config);
// now you can start ditto's sync
ditto.start_sync();Initializing LAN-Based WebSocket Connections
If syncing over LAN, ensure WebSocket connections are discoverable across peers within your local network:
val config = DittoTransportConfig()
config.connect.websocketUrls.add("ws://127.0.0.1")
ditto.transportConfig = config
ditto.startSync()Listening for Connections
import { TransportConfig } from '@dittolive/ditto'
const config = new TransportConfig()
config.listen.tcp.isEnabled = true
config.listen.tcp.interfaceIP = '0.0.0.0'
config.listen.tcp.port = 4000
ditto.setTransportConfig(config)
ditto.startSync()Combining Multiple Transports
import { TransportConfig } from '@dittolive/ditto'
const config = new TransportConfig()
if (isNode() && OS === 'darwin') {
// 1. Enable All Peer to Peer Connections (not in a browser environment)
config.setAllPeerToPeerEnabled(true)
// 2. Listen for incoming connections on port 4000
config.listen.tcp.isEnabled = true
config.listen.tcp.interfaceIP = '0.0.0.0'
config.listen.tcp.port = 4000
// 3. Connect explicitly to remote devices
ditto.setTransportConfig(config)
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")
}
}