A poll-free option for Core HTTP API clients

Dropbox has added longpoll_delta to the Core HTTP API, an endpoint that uses HTTP long polling to tell the caller when files in a user's account have changed. The endpoint is already in production behind the Sync API on iOS and Android, where it is responsible for noticing changes as soon as they happen.

Pairing long polling with delta

longpoll_delta is designed to work alongside the existing delta endpoint. A call to delta returns a set of changes plus a cursor; passing that cursor on the next delta call restricts the response to changes that occurred after it. The usual pattern is to call delta on a schedule, generally about every five minutes.

The new call takes that cursor and blocks until either new changes appear or a timeout is reached, which removes the need for periodic polling and gives low-latency notification. Once it returns, an immediate delta call retrieves the actual changes.

Calling it without SDK support

Python developers currently have to call the endpoint directly, since longpoll_delta is not yet exposed in the Python SDK. The sample below uses the Requests library to do this and prints an update to the console whenever a file is created, modified, or deleted in Dropbox.

import dropbox
import time
import requests
 
app_key = '<YOUR APP KEY>'
app_secret = '<YOUR APP SECRET>'
 
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
 
print 'Go here and "allow": %s' % flow.start()
code = raw_input('Paste in your authorization code: ').strip()
 
access_token, _ = flow.finish(code)
 
client = dropbox.client.DropboxClient(access_token)
 
cursor = None
while True:
 result = client.delta(cursor)
 cursor = result['cursor']
 if result['reset']:
 print 'RESET'
 
 for path, metadata in result['entries']:
 if metadata is not None:
 print '%s was created/updated' % path
 else:
 print '%s was deleted' % path
 
 # if has_more is true, call delta again immediately
 if not result['has_more']:
 
 changes = False
 # poll until there are changes
 while not changes:
 response = requests.get('https://api-notify.dropbox.com/1/longpoll_delta',
 params={
 'cursor': cursor, # latest cursor from delta call
 'timeout': 120 # default is 30 seconds
 })
 data = response.json()
 
 changes = data['changes']
 if not changes:
 print 'Timeout, polling again...'
 
 backoff = data.get('backoff', None)
 if backoff is not None:
 print 'Backoff requested. Sleeping for %d seconds...' % backoff
 time.sleep(backoff)
 print 'Resuming polling...'

The full documentation for longpoll_delta covers the endpoint's parameters and responses, and the developer forum is the place to send feedback.