Insights

Creating a Real-time Voting System on Apple Watch

4 min read Michael Carroll on Jan 18, 2016

Ever since the Apple Watch was announced, I’ve thought it was an interesting device for sending and receiving quick bites of information, which makes it a perfect match for PubNub technology. In this article, I’d like to share one of my ideas, a real-time voting app using the PubNub Mac, iOS, and the beta watchOS SDKs.

The Big Idea – Project Overview

The real-time voting app lets the owner of the server create short polls to be shared with other users through an app. This would be perfect for a classroom setting.

The project has a simple three-component architecture:

  • Poll host – small desktop app running on a Mac OS X laptop. It is responsible for active poll management.
  • Attendee app – iOS application which is running on attendee’s device and allow to react on announced polling.
  • Observer – small watchOS app running on Apple Watch and allow to observe votes results in real time.

Each component above communicates using the PubNub SDK libraries for each platform: Mac, iOS, and watchOS. The workspace is configured to use static libraries to demonstrate separate binaries for each component.

Note: It is strongly recommended not to use PubNub client directly on the Apple Watch and to organize project architecture in a way where paired device will be the core for data retrieval. Apple Watch in it’s default configuration will turn off screen after 15 seconds of no interaction – this is how Apple tell us not to run any long-running tasks (like subscribe from PubNub client) on Apple Watch.

 

Mac OS Desktop Poll Host

The poll host allows the user to publish a new poll with a question and up to 5 possible responses.  The app itself shows statistics real-time in a table. Statistics for attendees and observers is aggregated and sent every half second.

If the previous poll wasn’t completed properly, after restart the app will pull out information about it and the last statistic which it generated.

Note: If the host is offline, votes from attendees won’t be saved (saved only through aggregated updates). Make sure you start the host app first.

 

Attendee iOS App

The main real time iOS app allows the conference attendees to give their vote for one of options provided by the poll host. As soon as the publish operation is completed, the user will be presented with bar chart which will be updated every time the host sends out aggregated statistics information (currently set to every 500 ms).

As with the real-time host app, the attendee’s iOS app uses searches through the previous session to pull out poll information even if the poll was started before user the installed the app. The latest statistic will be pulled out as well.

Note: This is a simplified real-time polling system which doesn’t track whether the conference attendee already voted or not. In a real system you would need to prevent ballot stuffing.

The app (with proper PubNub account and provisioning configuration) provides push notifications for each time when new poll announced or completed.

 

Apple Watch Observer

This is an Apple Watch Extension which allows the user to observe the polling process in real time using bar chart on the screen. As with the host and attendee’s apps, the watch extension is able to catch up on previously started votes and receive latest aggregated statistics.

 

Compiling and Running the Apps

Let’s compile and run the sample apps. The source code is available on this github repo.

To be able to deploy on device and launch the host app, you need to prepare an XCode workspace by running the CocoaPod command from the repository root folder in your terminal:

pod install

After everything will be completed, open the PubNubPoll.xcworkspace workspace and use configured targets for deployment and host launching.

 

Running the Mac App

To run the Mac app, choose the target as Mac then run it:

Poll XCode Mac

You can set a poll in the Mac app and press the Start poll button to start it.

Mac poll host

 

Running the Watch App

To see the current stats on your watch, choose the target as watch and run:

Apple Watch poll target

 

 

Apple Watch poll simulator

 

Running the iOS App

To vote, chose the Xcode target as iPhone and run it:

Poll XCode iOSPoll iOS simulator

When you vote, the watch will update with the current stats:

Apple Watch demo

 

The PubNub API on the Apple Watch is the same as iOS, only the compilation is different. This SDK is not beta, however there is no Xcode support for watchOS automated testing. We can support watchOS but we can’t test against it. We have filed a ticket with Apple and will update the SDK when Xcode changes. If you have any questions or want some new features, please let us know.

0