rsms

Behind the Facebook Desktop Notification

Me and a friend looked at what the newly released Facebook Desktop Notification is doing (a simple OS X application which gives you seemingly real-time visual notifications when something happens on Facebook). Since it's very quick at displaying notifications and since people have requested a pub/sub API to Facebook for quite some time, we thought that hey, maybe they're trying out a new notify-API! Let's reverse engineer it..

Well, turns out they are actually polling every 30 seconds. Wow. With 100m+ users, thats a lot of queries.

The application uses fql.multiquery to send optimised queries and also asks for JSON responses in order to keep the bandwidth usage down.

...
messages:
SELECT thread_id, subject, snippet_author, snippet, unread, updated_time 
 FROM thread 
  WHERE folder_id = 0 AND updated_time > 1253102646
...

The response looks like this, if something new has happened since last time we checked:

{"name":"messages",
 "fql_result_set":[
  {"thread_id":"1040528110203",
   "subject":"yoooo",
   "updated_time":1253102646,
   "snippet":"igen, hej hej! :)",
   "snippet_author":1234567890,
   "unread":1
}]},
...
[{"name":"pic",
  "fql_result_set":[
   {"name":"Rasmus Andersson",
    "uid":123456789,
    "pic_square":"http:\/\/profile.ak.fbcdn.net\/v223\/550\/18\/q728642302_410.jpg"},
   {"name":"Jon \u00c5slund",
    "uid":1234567890,
    "pic_square":"http:\/\/profile.ak.fbcdn.net\/v225\/558\/26\/q803274534_5076.jpg"}
...

In other words; they return the minimum amount of data in a single response needed to represent the event — subject, time, message excerpt and the parties involved in the discussion.

Well, most if this is fairly old news, but the fact that themselves (Facebook) release an application which polls the API at such a high rate indicates that we (merely mortals) can do so too. Opens up for close-to-realtime applications.