[MUSIC PLAYING]
PETE LEPAGE: Well, hello everybody, and welcome to
another edition of Chrome Apps Office Hours.
We've got--
joining us today, we've got Joe Marini.
Joe, do you want to introduce yourself?
JOE MARINI: Sure.
Thanks, Pete.
I am Joe Marini.
I'm a developer here on the Chrome team and I work on
Chrome Apps.
PETE LEPAGE: Sweet, and I'm Pete LePage.
So today we're going to be talking about the new Alarms
API, which allows you to create specific events that
fire at specific--
or create events that fire at specific times
or on regular intervals.
With any kind of native or installed app, one of the
things you might want to do is have regular events that go
pull for data, or maybe you're writing something like an
alarm clock, and you want to have the application notify
the user of a specific thing that happened, or that it's
time to get up and it's time to do something.
So with the Alarms API, you can actually
go in and do that.
You can much more easily say, hey, even though my
application is not running right now, I want to go and
fire a specific event and have something
happen with that event.
So it's a pretty cool API, pretty useful to use and it's
really, really simple.
Normally, our shows run about 30 minutes.
I think today we may actually even finish up in about 15
minutes because this is a really simple API.
Why don't we go ahead and jump in, and we'll take a look at
how the API works.
So let me just open these guys up here so that we can get
this going, and we'll have a look here.
So there's a relatively easy way of creating an alarm.
It's chrome.alarms.create and you give it a name, which is a
string, and an alarm info, which is an object.
So the string can be anything.
You just use that to identify what the alarm is, and then
the alarm info has the details about how
the alarm gets fired.
So there's a couple of things that you could put in here.
So first thing you could have is when.
So that's a specific time you want the alarm to fire.
So if you want it to fire at say, now plus 30 minutes,
you'd say, date.now times 60 seconds times 1,000 times 30.
So that gives you the 30 minutes because it gives that
time n number of milliseconds past the [? epoch. ?]
The other option is you can say delay in minutes, which
provides the, hey, I want to fire in 30 minutes or I want
to fire in 45 minutes.
So that's just a double if you want a specific, hey, just
hang out for a certain amount of time and then
fire this guy off.
You can use either delay in minutes.
And then finally, period in minutes is another one that
says, hey, I want you to refire this event every so
often, so however many minutes you want it to refire in.
Now, all three of those are optional, but realistically,
you need at least one of them provided, so you need either
to provide the when or the delay or the period.
You could also do two of them, so that you could say when
plus period or delay in minutes plus period.
So you could say, hey, I want you to fire this event in 30
minutes and then wait 30 minutes and fire it again and
fire it again and fire it again.
So that way you can have that recurring, hey, go check to
see if there's any new data or maybe you want to go and do
something else with that.
So like I said, they're all optional, but you should fire
at least one.
If you don't apply anything, the alarm just fires right
away, which is kind of pointless.
And then, of course, you need to listen for the event.
With the Alarms API, it fires an event when the alarm fires.
That was repeating myself just a little bit, but that's OK.
And so you just say chrome.alarms.onAlarm.addevent
and then the function name that you want to do.
And of course, you can do an inline function, and you'll
see that in a sec when we take a look at the demo.
And it passes one parameter, the alarms parameter.
And in the alarms parameter, it gives back the effectively,
the alarm info that you provided.
So how often you want it to fire as well as
the name of the alarm.
So that's it.
It's pretty simple.
Joe, is there anything you want to add to that?
JOE MARINI: Yeah, just a couple of quick notes.
First, we should point out that there's actually no
strict guarantee that the alarm will fire exactly when
you scheduled it to.
PETE LEPAGE: Good point.
JOE MARINI: Sometimes, due to performance reasons, if
there's a lot of alarms scheduled, for example, we may
delay the alarm some arbitrary small amount of time so that
all the alarm events don't pile up in the system.
The other really cool thing I love about this API, is that
it works even when your app is not running.
You skip an alarm, and then the user closes your app, and
the alarm comes along and your app will actually be woken up
and told that the alarm fired.
In past programming paradigms, sometimes developers would try
to find a way to keep their app running all the time so
they could do some kind of polling or whatever.
You're using the Alarms API and some others we have
available as well, it makes it so you don't
have to do that anymore.
You can just schedule a periodic event that says,
regardless of whether my app is running or not, I need to
know when this thing fires.
And this is a really great utility for doing that.
PETE LEPAGE: Exactly.
Yeah, that's a good point.
In the past, you might have done things like set interval
or set timeout to go and update.
That works, and it's functional, but it's a lot of
code and it's just more work than it's worth.
With this--
JOE MARINI: And once again, it doesn't work when your apps
not running.
If your app gets terminated for some reason or whatever,
that code just-- the minute it goes away, your code goes
away, so Chrome Alarms API helps solve that problem.
PETE LEPAGE: Exactly.
So I'm going to come out of my slides here, and let's just
have a look at a demo that I've got here.
So we'll take a look at the code first because I think the
code is most interesting.
So there's a couple of things that I want to point out
within the code, so within the manifest file, you need to ask
for the alarms permission.
You need to just say, hey, look, I want to be able to
fire alarms.
So relatively simple permission ask here, so I just
said alarms.
And then, the way I design this app, I wanted the alarms
to be able to be handled whenever
the app wasn't running.
And this was something that, as Joe just mentioned, if you
wanted the alarms to be handled when the app is not
running, you need to put the event listener into the main
JavaScript files, so into the main JS, into the place where
you're listening for all of the events.
So you can see here, I've got my chrome.alarms.onAlarm dot
add event listener.
And I'm just doing it as an inline function, so I say,
function alarm, looking for that alarm.
And I'm just going to do a console log just because that
way I can see what's happening.
And in this particular case, I'm just going to throw up an
old style WebKit notification.
Really, the better way to do this-- and I threw this
together in about five minutes earlier today--
but the better way to do this would have been to use one of
the new notification APIs, which we talked about last
week, or maybe, I guess, it was the week before.
One of the cool things about that is that you can use those
really easily, and they just came out of experimental, so
you can use those without the experimental flag.
So then finally, we've got--
we show our notification, so create notification.
And I'm just using the alarm name because I just wanted
something simple.
And for the body of the message, I'm just going to
stringify the object so that we can see what's going on,
and then we show it.
Within the actual app--
if we have a look at the HTML for a sec, you can see it's
pretty simple.
I've just got a form where I take in the name for the
event, a time, and an add alarm.
So that within the actual application code, I've got the
add for time, where I go and get the date and time, or add
for delay so that I can say, hey, I want this to fire in x
many minutes.
So let's take a quick look, and I'm going to set this guy
for delay for two minutes and add alarm.
So you notice that the time went away, so we've
got this guy going.
So in about two minutes, up in this upper right hand corner
that you'll see, I think it's--
yeah, right above my head, right about there.
We're going to see an alarm notification that'll pop up.
We could also use the specific time so I want to
set this for 12--
whoops--
for 12.
Let's set this for 12:15 PM and add alarm.
So I've added that, and I had everything spit stuff out to
the console so we could see what's going on.
So we can see that there are alarms here that are
available to us.
So the first time when I click it, you can see-- and let me
just make this text a little bit bigger-- you can see that
we have all of our alarms, which I just used
chrome.alarms.getall, and then printed the output.
So we've got our delay one, along with the scheduled time.
And the second time we did it, we've got for a specific time,
and the time that it's going to fire.
So there's the scheduled time and the name of it.
Now, I think I see a bug in my code here.
I was playing with this a little bit earlier, and this
has to be a double, and this happens to be a float.
So we may end up having this guy not quite work, so we'll
see what happens in a sec.
I think I know why.
So the other thing that I want to have a look at, is if we
inspect the background page here, you can see that no
events have been fired.
When events get fired, we'll have that guy fire off.
So if we go back and have a quick peek at my code--
let's see, so we're doing our delay in minutes.
We're doing a parse int with the for delay and that
shouldn't be a--
Alarm fired, but we didn't get our notification popping up.
So you can see that we got our delay popping up, but
actually, this is maybe not what I was thinking it was.
Joe, do you see anything with this?
JOE MARINI: No, not right off the bat, I don't.
PETE LEPAGE: All right.
Well, we can see that the event fired, so that's the
important thing.
JOE MARINI: That's the important part, exactly.
PETE LEPAGE: And the important part here is that when the
alarm fires, its firing off in the main event so it's being
handled in the main event.
That means, like I said, you can have
these events fire off.
You can go get new data.
You can pop up notifications.
You can do anything that you need to do so that your users
know what's going on, and that your users know all the stuff
that they need to know about or that your
app stays up to date.
It's a really great way of capturing data locally and
catching stuff going on.
So Joe, anything you'd like to add?
JOE MARINI: Well, of course, the API gives you ways to see
what alarms have been scheduled, clear any specific
alarms, clear all the alarms.
It's a pretty full featured API.
PETE LEPAGE: Yeah.
All right, so let's say pop over to questions, and we can
bring up their questions URL because I know that we had a
few questions already.
And we'll take some of your questions.
So you can ask your questions, the link is that way.
There we go.
The link is down that way if you have questions that you
want to ask about this.
The first question we'll take, can we use the API to remove
cookies from other domains, either time based or click
based removals or anything like that?
And the answer to that is no.
Joe, do you want to collaborate on
that a little bit?
JOE MARINI: You have to remember that one of the
security models that we built into Chrome Apps is that
cookies are all isolated from each other, so you can't just
go poking into other cookies.
So the short answer is no.
There's an isolation model in place.
You've only got access to any cookies that you've personally
set and [INAUDIBLE].
The whole cross domain thing gets really hairy, really
quickly, so for security reasons, no.
PETE LEPAGE: Exactly.
So this is not something that you're going to use for
removing cookies or anything like that for other places.
You might use it if you want to remove your own cookies,
but that's about it.
So the second question here, are there any use cases for
alarms that you feel have been underutilized for developers
or that you'd like to see more of?
To be honest, I don't think we've seen many
uses of this API yet.
So--
JOE MARINI: It's still relatively new.
I'm actually kind of excited to see how it gets used, but I
think there are some pretty usual suspects use cases, but
I'm actually kind of excited to see what comes along that I
hadn't thought of.
PETE LEPAGE: Yeah.
The main things that come to mind for me are things like
caching and storing data.
So for example, a news app or maybe a photos app, might want
to store the stuff that I'm going to
look at every morning.
So as I'm on my way into the work or maybe I'm on the train
or something like that, all of the data that I need is always
up to date so that I can be like, great, perfect.
And maybe that's something that the user sets and the
user says, hey, I want to update this data every morning
at this particular time, or maybe--
JOE MARINI: It also might be useful to see how the data
changes throughout the day.
That same snapshot of data across every hour or so.
PETE LEPAGE: Exactly.
The other thing that you could use it for is listening for
and updating data from the users side up to the server.
Because maybe it's something that the user's
storing stuff locally.
Think something like Google Docs, where as I'm typing and
storing data locally, maybe I want to save that up to the
cloud and rather than push it up every single keystroke or
every bunch of keystrokes, you can have an alarm fire for
every three minutes or five minutes or 20 minutes, so that
all the data gets stored up to the cloud.
But again, pushing and pulling data is, I think, one of the
primary things that it's going to get used for, but writing
an alarm clock.
Perfectly great example.
JOE MARINI: Anything that requires periodic code
execution is a pretty good use case.
PETE LEPAGE: Exactly.
So Gavin Good from Kingston wants to know, how do you turn
alarms on and off?
So the way that you turn an alarm on and off, from a
developer point of view, is you add the alarm, and then
you can do a clear alarm.
So if we have a look at the API again, let me just pull
that particular slide up.
And let's see here, I think--
Here we go, yeah.
So you can see here on this particular--
there's a clear with a clear name.
So you could provide a name of the alarm you want to clear,
so that'll turn it off.
And then you just then add a new one.
JOE MARINI: There's also a clear all.
You can also--
There's a clear all that will clear all of them out.
PETE LEPAGE: Exactly.
As a user, you can't turn them off, but from a
developer you could.
So if you want to be a good developer, it's probably a
smart idea to allow the user some way of turning those off
if it's not a core piece of functionality for your
application.
So if it's maybe just popping up an alert when there's new
email or something like that, then, yeah, that's something
you want to give a choice to.
Or maybe you want to allow the user to choose how often they
want to poll.
So instead of saying, hey, we're going to poll every 30
seconds or 90 seconds or minute, you can
definitely do that.
All right.
Let's see, are there any other questions before
we wrap up for today?
I think that looks like all the questions.
So Joe, thanks for joining me this morning and--
JOE MARINI: Thanks for having me.
I appreciate it.
PETE LEPAGE: Thanks to everybody for turning it.
We actually are going to be back again tomorrow.
We have a special edition of our Chrome Apps, and we're
actually going to be moving our show to Wednesdays now,
for a whole bunch of reasons.
So we hope you'll join us starting tomorrow, and we look
forward to seeing you.
All right, well, thanks everybody.
JOE MARINI: Thanks, everyone.