Overview

Using the connectionRequestHandler API method, you can intercept new peer connections and make decisions to accept or reject them based on the peer metadata set for a given device. (See Setting the Connection Request Handler)

Within the connectionRequestHandler you can use the connectionRequest parameter to access information about the peer connection request. (See Reading Connection Parameter)

  • Peer key string — The identifier encoded as a string that uniquely identifies the requesting peer once discovered within the mesh.

  • Connection type — The type of connection the peer is requesting.

  • Peer metadata — The information provided by the end user associated with the peer making the request. (For more information on peer metadata, see Using Mesh Presence)

Setting the Connection Request Handler

To accept or reject an incoming connection request:

Once you’ve set the request handler, end users must always make a choice to accept or reject incoming connection requests; otherwise, potential issues like request deadlocks and connection timeouts may result.


ditto.presence.connectionRequestHandler = { connectionRequest in
    if (true) {
        return .allow
    } else {
        return .deny
    }
}

Reading Connection Parameter

To see specific information associated with a requesting peer connection


ditto.presence.connectionRequestHandler = { connectionRequest in

    print(connectionRequest.peerKeyString)
    print(connectionRequest.connectionType)
    print(connectionRequest.peerMetadata)

    return .allow
}