Pollymer (not to be confused with Polymer, which it predates) is a general-purpose AJAX library designed for long-polling any protocol. Before you set off to write manual long-polling code again, consider using Pollymer for the task. Features include:

  • Automatic retrying of failed requests, with an exponentially increasing delay between retries.
  • Automatic re-polling on success, separated by a small randomized delay. Pass a function for the URL to be able to dynamically adjust the polling target.
  • Workarounds for browser “busy” indications.
  • JSON-P fallback for cross-domain requests on older browsers (requires server support).

Long-polling is great for doing realtime in the browser, but most long-polling code out there is not very reusable. Often, such code is buried within larger JavaScript frameworks, and designed to speak special protocols. Since every long-polling application needs the same retry/backoff logic, we figured it would make sense to put the common parts into a library we could all share.

Pollymer is useful for non-long-polling requests, too. For example, setting maxTries to 2 can ensure a request survives temporary errors, much like how database drivers often try connecting twice before failing.

Code is here. The Readme should explain everything. License is MIT. Enjoy. If the library doesn’t work with your particular application protocol or flow, we’d love to hear about it.