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 theDittoTransportConfig
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.Syncing with Ditto Server / WebSocket Connections
To sync with the Ditto Server or to initialize a WebSocket connection, you need to add awebsocketURL
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.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.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.
- Define the TCP listening port (e.g.
12345
) as described in Listening for Connections. - 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
. - Set
TRANSPORTS_DISCOVERED_PEERS
on all the other devices in the mesh to make them connect to the hub. See code examples below:
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.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.Replacing mDNS Discovery
Supported in SDK version 4.12.0 and later.
- Disable peer-to-peer LAN in the
TransportConfig
. - 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.
- Read the
discovery_hint
using the following DQL query:
SELECT * FROM system:transports_info WHERE _id = 'discovery_hint'
- Get the device’s IP address on the local network.
- 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.) - Update
TRANSPORTS_DISCOVERED_PEERS
with the full list of discovered peers as the device receives discovery information from other devices.
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.