Multipeer Discovery, Time Synchronization

A deeper look at the Discovery phase of iOS Multipeer and a simple way to synchronize clocks.

Kyle Beyer
Hacker School

--

This is the third post in a series where I’ve been reflecting on my experience and sharing about a project at Hacker School.

Brief Weekly Recap

In my 3rd week here at Hacker School, I had the opportunity to collaborate on several different projects, witness the magic of Mary building Space Invaders in 30 minutes, meet more alumni, and continue progress on my research of the iOS Multipeer API.

One of the students I paired with this week was Costa. Costa has some really great ideas. We looked at ways to understand cascading DOM events better and started prototyping a micro CSS framework.

Another fellow Hacker Schooler I collaborated with was Bert. Bert and I had a fun discussion about how to effectively use the MVC software architecture pattern in iOS applications and looked at an application he is building as an example.

Multipeer Discovery

As I continued researching the iOS Multipeer API, my focus this week was on the first phase of creating a peer-peer connection. During the discovery phase, a device can browse for nearby peers who are advertising their availability.

Key events during the discovery phase.

To start, I instrumented each of the key steps in the discovery phase using the event visualization tool I built last week. My objective was to capture amount of time for each step and the invitation send/accept process as a whole.

Visualization of event timing.

I used my iPhone as one test device, and a simulator as second device. The first thing I noticed was that the events appeared to be happening out of order and the duration of events was quite different when I swapped which device sent/received the invitation.

The reason was that the clocks of the two test devices were not perfectly in sync. This caused any visualizations of events that were dependent on both clocks to be distorted and to create a incorrect perception for the order of events in some cases.

Time Synchronization

In order to capture accurate timing, it was clear that I would need to find a way to ensure the event timestamps from any test device were in reference to a common absolute. Rather than implement the Simple Network Time Protocol (SNTP) commonly used on the Internet, I opted for a simpler approach which has proven effective for networked games and would provide the attributes needed in this case.

Simple time sync algorithm

Once a connection is established between devices, a series of messages are exchanged to implement the above algorithm. This has shown to synchronize the times within 100ms. Although both devices use the same time server (Apple’s) to update, the offset has been ~500ms which is significant in the context of creating shared experiences that feel near realtime to a human. The value will extend beyond the purposes of instrumentation.

Screenshot of example difference after time sync.

Next Steps

The event visualizations look much better and the accuracy of duration events improved. My focus will shift now to evaluating the different types of data that can be exchanged once a peer-peer session is created (i.e. — message, file, stream).

--

--