Customizing Transports
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.
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.
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.
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:
Connecting to Remote Small Peers
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.