When using the Multipeer Connectivity framework to connect multiple iPhones / iPads using an "every device advertises a service + everyone invites everyone" implementation, I sometimes see a device failing to connect even on perfect Wi-Fi networking conditions.
Here is an example project that can be used to reproduce the issue:
https://github.com/ralfebert/ConnectedColors/
The code to handle the Multipeer Connectivity session is here:
https://github.com/ralfebert/ConnectedColors/blob/main/ConnectedColors/ColorMultipeerSession.swift
I reported the following FB report to Apple about this, assuming this is a bug and not incorrect usage of the framework / expected behavior.
Question: Is this indeed a bug in the Multipeer Connectivity framework, or am I using the framework incorrectly? If it is a bug, is there a workaround that can be used to ensure devices can always connect in good network conditions?
FB9703076: Multipeer Connectivity peers sometimes failing to re-connect in perfect network conditions
When using the Multipeer Connectivity framework to connect multiple iPhones / iPads using a "every device advertises a service + everyone invites everyone" implementation, I sometimes see device failing to connect even on perfect wi-fi networking conditions.
Steps to reproduce:
- Run the attached ConnectedColors example project (https://github.com/ralfebert/ConnectedColors/) on multiple devices (it's easier to reproduce using at least 3 devices).
- Usually they all connect successfully initially and you can change the color on all devices.
- Disconnect and connect one of the devices by force-quitting the app or by suspending it (both can lead to the issue occurring).
- After some tries, you'll see the device not being able to connect.
The app trys to re-invite known-but-not-connected peers every 5 seconds. Sometimes this helps to recover such a failure, but often it becomes completely stuck.
Here is a video of such a connection failure occuring with 3 devices: https://www.youtube.com/watch?v=MDg2BlN65R4
In those cases, you can see the failing re-invitation attempts together with these errors in the log:
2021-10-14 12:33:01.808847+0200 ConnectedColors[1505:832300] [connection] nw_endpoint_handler_set_adaptive_read_handler [C43 fe80::60:594d:64d9:7d95%en0.49772 ready socket-flow (satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, ipv6, dns)] unregister notification for read_timeout failed
2021-10-14 12:33:01.809123+0200 ConnectedColors[1505:832300] [connection] nw_endpoint_handler_set_adaptive_write_handler [C43 fe80::60:594d:64d9:7d95%en0.49772 ready socket-flow (satisfied (Path is satisfied), viable, interface: en0, scoped, ipv4, ipv6, dns)] unregister notification for write_timeout failed
2021-10-14 12:33:01.845341+0200 ConnectedColors[1505:831238] didReceiveInvitationFromPeer <MCPeerID: 0x283678110 DisplayName = iPad>
Related resources
The example project comes from my introductory article about using the Multipeer Connectivity framework: https://www.ralfebert.de/ios/tutorials/multipeer-connectivity/
The setup from the ConnectedColors project is very similar to the one from the 'Creating a Collaborative Session' example project:
https://developer.apple.com/documentation/arkit/creating_a_collaborative_session