Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Justin Ribeiro, a software engineer, building on the edge with his company, Stickman Ventures, Inc. He will hack just about anything–code, hardware, house, car, and kids’ toys. You can find him on Twitter @justinribeiro.

In my first post, I discussed getting started with the Mirror API and handling the inserting of timeline items. In this post, we’re going to explore listening for events from actions taken on cards and we will work with contacts.

Setting up a Subscription

While inserting a timeline item can be useful for your Google Glass end users, allowing them to take action on that item is more useful. What you have do is setup a subscription.

What exactly is a subscription? When a user takes action on timeline cards or when a user location updates, this triggers a notification. When you setup a subscription, your Glassware can receive a ping at your defined endpoint with a set of data that you can decide to act on. Let’s walk through setting up a basic subscription:

One of the big things you have to keep in mind is the callback URL. Whatever your callback URL, it needs to be running under SSL. If your callback URL is not running, you can temporarily use the SSL proxy provided by Google to do so. An example of it in action:

Note, this proxy is not designed for production use and should only be used for testing.

Getting data from user actions

With the subscription setup, you can start listening at the endpoint. Notifications from the Mirror API will come to use in the form of POST data that you can operate on.

Let’s presume that your user has taken an action on your timeline card and decided to reply. You’re listening for INSERTs. The data sent to your endpoint might look like the following:

It’s going to vary based upon your implementation language, but pulling this information from the callback is fairly staightforward. In PHP, it looks something like:

Different actions result in different payloads. In this case, the end user has used the reply action on a timeline card. Look here for a full list of subscriptions.

Now that you have a payload you can operate further. Sticking with this example, let’s presume you want to get the reply text that was sent to your card. You can use the ItemId to pull the information by using timeline->get():

Consider the length of operation

Before taking action on this payload, make a determination as to what you’re going to do with this information. Why you might ask? That’s because you need to tell the Mirror API that everything is okay by returning 200 OK within 10 seconds. If you have a process that is going to run long, you might need to spin it into a job or thread that can operate outside the bounds of the callback.

In a land of updates, you need a contact

Although you can listen for updates for the timeline, your Glassware can’t access data that it did not create. What you need is a middle man, and that middle man is a contact.

Inserting a contact allows the end user to share from cards that have sharing enabled or other relevant data. Let’s insert a contact and walk through the setup:

This is the most basic of contacts. Of note, you have to set an image URL for use with your contact. This image needs to be 640px by 360px.

Mime Types for your contact

Depending upon your use case or user story, you may want your contact to only accept certain types of data. You will help not only yourselves, but also the end user by limiting your contact to only the mime types that you can accept when using a share:

Using the “Post an update to…” voice command

What if you want your contact to accept voice commands? You can extend your contact’s functionality by allowing it to accept different types of commands beyond the share option on timeline cards. You can patch your contact to accept the note and post an update.

This results in your contact now appearing within the “Post an update to…” voice menu.


You can process data from the user the same as you did before; by listening for an INSERT of the timeline card (it’s type would be LAUNCH) and then pulling the relevant itemId and text associated for action by your application.

Why patch your contact and not update

In the previous two examples, I’ve used the contacts->patch method of updating the existing contact. If you’ve read the documentation, you’ll quickly note that you can also use the contacts->update method to update an existing contact in place. Why patch? The primary reason is because you don’t have to send all required data back to your contact; you can send only what you want to change on the existing contact.

Putting subscriptions and contacts together

As you can see, once your end user starts interacting with your contact, you can use your previous subscription to receive notifications and act upon them. These notifications, as well as those generated from the timeline cards you’ve inserted, allow you to easily send to Glass as well as receive responses from Glass.

There is a lot more the Mirror API can do for you. These posts I’ve written just scratch the surface. For more information and resources, be sure to check the Glass Developers documentation, visit Stack Overflow, or look below at the Glass and PHP resources from Safari Books Online.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Web Designer’s Guide to Google Glass is a hands-on guide to optimizing web sites for Google Glass will show you how to take advantage of the latest advancements of this tiny screen, including watching videos and viewing forms. You’ll also take a look at the Mirror API and building web-based native apps for Google Glass, and get insightful tips from an expert designer on avoiding web design pitfalls.
Fluent Conference 2013: JavaScript & Beyond Complete Video Compilation is for anyone who missed the Fluent Conference 2013 in San Francisco this past May, allowing you to catch every workshop, session, and keynote with this complete video compilation of the event. Catch up with this year’s lineup of speakers—seasoned pros as well as notable newcomers—as they share their expertise with JavaScript, HTML5, CSS, and related technologies that power the Web, and don’t forget about Fluent Conference 2014.
Programming PHP, 3rd Edition is an updated edition that teaches everything you need to know to create effective web applications with the latest features in PHP 5.x. You’ll start with the big picture and then dive into language syntax, programming techniques, and other details, using examples that illustrate both correct usage and common idioms.

Tags: Contacts, Glass, Glassware, Google Glass, php, Subscriptions,

Comments are closed.