• Stars
    star
    133
  • Rank 270,999 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

General-purpose AJAX/long-polling library

Pollymer

Authors: Justin Karneges [email protected], Katsuyuki Ohmuro [email protected]

Pollymer is a general-purpose AJAX library that provides conveniences for long-polling applications, such as request retries, exponential backoff between requests, randomized request delaying, and workarounds for browser "busy" indications. It also implements multiple transports to ensure cross-domain access works in all major browsers.

License

Pollymer is offered under the MIT license. See the COPYING file.

Dependencies

None, but if you need to support a browser that does not natively provide support for JSON (such as IE7 or lower), then you should use the json2.js polyfill.

Available Transports

  • XmlHttpRequest - for same-origin requests on all browsers, and cross-origin requests on modern CORS browsers
  • JSON-P - for cross-origin requests on older non-CORS browsers such as IE7, IE8, IE9, and Opera < 12.00

Limitations

  • If the JSON-P transport is used, the request headers and body are subject to URI length limitations of the browser and server.
  • If the JSON-P transport is used, it may not be possible to inspect all of the response headers. At most, the server may provide "noteworthy" headers within the JSON-P encoding.

Usage

For use in a browser script tag (Pollymer becomes a global variable), use the pollymer-1.x.x.min.js file in the dist/ directory. The non-minified file and sourcemap are also available.

For browserify, use npm install pollymer, then var Pollymer = require('pollymer');.

For jspm, use 'jspm install github:fanout/pollymer', then import Pollymer from 'fanout/pollymer';.

var req = new Pollymer.Request();
req.on('finished', function(code, result, headers) { ... });
req.on('error', function(reason) { ... });
var headers = { ... };
var body = 'some data';
req.maxTries = 2; // try twice
req.start('POST', 'http://example.com/path', headers, body);

Methods of Request Object

  • on(event_name, callback) - Add callback for event:

    • event_name: name of event
    • callback: method to call when event occurs
    • This method returns a function that can be called to remove this callback from the event.
  • available events:

    • 'finished': function(code, result, headers)
      • code: HTTP status code
      • result: JSON object or string
      • headers: hash of key/value strings
    • 'error': function(reason)
      • reason: Pollymer.errorType
  • off(event_name) - Remove callback for event.

    • Alternatively, call the function that is returned from on().
  • start(method, url, headers, body) - start request

    • method: name of method (e.g. 'GET')
    • url: string url, or function that returns a string url if called
    • headers: hash of key/value strings (optional)
    • body: string body data (optional)
    • Sometime after the request has been started, a finished or error event will be raised and the object will return to inactive state (unless the recurring flag is set, see below).
    • The start method may be called again once the request has completed (unless the recurring flag is set, see below). If called again on the same object, a short random delay will be added before performing the request.
  • retry() - Attempt the exact same request again.

    • Normally, Pollymer will automatically retry a request that it considers to be a failure, but this method may be used if the application needs to retry the request for any another reason. Retries have an exponentially increasing delay between them. Do not use retry() if the previous request attempt was considered to be successful, as it will add penalizing delays that you probably don't want in that case.
  • abort() - Stop any current request.

    • This returns the object to inactive state.

Properties of Request Object

Properties are simple members of the object. They can be set directly:

req.rawResponse = true;

or passed in a hash during construction:

var req = new Pollymer.Request({rawResponse: true});
  • rawResponse: boolean. By default, this library will parse response body data as JSON and return an object to the application. Set the rawResponse property to true to disable this behavior and have a string returned instead.

  • maxTries: int. The number of tries a request should be attempted with temporary failure before raising an error event. Set to -1 to indicate infinite attempts. Default is 1.

  • maxDelay: int. The maximum amount of random milliseconds to delay between requests. Default is 1000.

  • recurring: boolean. If set to true, then after a request finishes with a code between 200 and 299, and the finished event has been raised, the same request will be started again. This allows Pollymer to automatically poll a resource endlessly. Pass a function as the url argument in order to be able to change the url between polls.

  • transport: Pollymer.transportType. Explicitly set the transport to use. Default is transportType.Auto, which automatically chooses the best transport when the request is started.

  • withCredentials: boolean. If set to true, and the request will be a cross-origin request performed over an XmlHttpRequest transport using CORS, then the withCredentials flag will be set on the XmlHttpRequest object to be used. Use this flag if the CORS request needs to have HTTP Cookies and/or HTTP Authentication information sent along with it.

  • timeout: int. Request wait timeout in milliseconds. Default is 60000.

  • errorCodes: string. The error codes and/or ranges that should cause automatic retrying. For example, to have all error codes in the 500-599 range cause an automatic retry except for code 501, you could set errorCodes to '500,502-599'. Default is '500-599'.

Retries

When a request fails at the transport level, or the request succeeds with an error code between 500 and 599, and maxTries has not been reached, then Pollymer will retry the request silently, with an exponentially increasing delay between attempts. In any other case, the request will succeed and the finished event will be raised. If the application determines that the response indicates a temporary error and should be retried with the same backoff delay that Pollymer normally uses, the retry() method may be used.

Request Reuse

If start() is called on an object that has completed a request and is now inactive, then a random delay will be added before performing the next request. This is ideal behavior when repeatedly polling a resource, so do try to reuse the request object rather than throwing it away and creating a new one every time. When the recurring flag is used, the effect is the same as if you had called start() again yourself after a request finished.

Be sure to recognize the difference between a retry and a reuse of the object. A retry implies that the previous request attempt was a failure, whereas reusing the object means the previous request attempt was successful. This distinction is important because it changes the way the delaying works.

JSON-P Protocol

This library supports JSON-P by supplying the following query string parameters in the request:

  • callback: the JavaScript function to call in the response script
  • _method: the method name (default GET)
  • _headers: additional headers encoded as JSON (default none)
  • _body: request body (default empty)

This protocol dictates that the presence of the "callback" parameter signifies the request as a JSON-P request. The remaining parameters are optional.

The server is expected to reply with a JSON object with fields:

  • code: the HTTP response status code
  • reason: the HTTP response reason phrase
  • headers: any noteworthy HTTP response headers (default none)
  • body: response body

All fields are required except for "headers". Example response:

{
  "code": 200,
  "reason": "OK",
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "{ \"foo\": \"bar\" }"
}

Logging

All logging coming through Pollymer is performed through the Pollymer.logger function, which is defined in a development build as

Pollymer.logger = function(type, message) {
    console.log("Pollymer: " + type + "-" + message);
};

In a production (minified) build it is set to null.

The consumer of the library is free to assign any function to Pollymer.logger.

More Repositories

1

django-eventstream

Server-Sent Events for Django
Python
650
star
2

zurl

HTTP and WebSocket client worker with ZeroMQ interface
C++
388
star
3

apollo-serverless-demo

Serverless GraphQL subscriptions
TypeScript
107
star
4

reconnecting-eventsource

A small decorator for the JavaScript EventSource API that automatically reconnects
TypeScript
95
star
5

websockhop

Network resilient WebSocket wrapper
JavaScript
83
star
6

webhookinbox

Receive HTTP callbacks and see the data in realtime
Python
80
star
7

python-faas-grip

FaaS GRIP library for Python
Python
67
star
8

django-grip

Django GRIP library
Python
66
star
9

condure

HTTP/WebSocket connection manager
Rust
63
star
10

js-grip

JavaScript GRIP library
TypeScript
47
star
11

kafka-sse-example

Expose Kafka messages via HTTP streaming API
Python
38
star
12

fanout-graphql-tools

GraphQL subscriptions using Fanout/Pushpin
TypeScript
32
star
13

node-faas-grip

FaaS GRIP library for Node.js
JavaScript
32
star
14

pygripcontrol

Python GRIP library
Python
31
star
15

serverless-websocket

"serverless" WebSocket example
Python
30
star
16

editor

Collaborative text editor using operational transformations
JavaScript
30
star
17

docker-pushpin

Dockerfile
26
star
18

go-gripcontrol

A GRIP library for Go.
Go
23
star
19

php-gripcontrol

A GRIP library for PHP.
PHP
20
star
20

pushpin-c1m

One million push connections on one server
PHP
20
star
21

leaderboard

Leaderboard example
JavaScript
20
star
22

express-grip

Express GRIP library
TypeScript
17
star
23

js-eventstream

Server-Sent Events for JavaScript
TypeScript
12
star
24

flychat

Serverless chat demo
JavaScript
12
star
25

tigase-zmq

Tigase ZeroMQ Gateway
Java
11
star
26

laravel-fanout

Fanout.io library for Laravel.
PHP
11
star
27

js-pubcontrol

JavaScript EPCP library
TypeScript
10
star
28

laravel-grip

A GRIP library for Laravel.
PHP
9
star
29

express-eventstream

Server-Sent Events for Express
JavaScript
8
star
30

pypubcontrol

Python EPCP library
Python
7
star
31

ruby-gripcontrol

A GRIP library for Ruby.
Ruby
7
star
32

qfiber

C++ coroutines library with channels
C++
7
star
33

todomvc-liveresource-react

JavaScript
6
star
34

headline

Python
6
star
35

js-serve-grip

Grip library for Express and Next.js
TypeScript
5
star
36

node-fanoutpub

Node.js FPP library
JavaScript
5
star
37

livecounter

Live counter API
Python
5
star
38

scaledemo

Python
4
star
39

audiostream

Stream live audio
Python
3
star
40

pyfanout

Fanout.io library for Python
Python
3
star
41

engine.io-as-websocket

Wraps Engine.IO to make it usable with the same API as WebSocket
JavaScript
3
star
42

php-pubcontrol

EPCP library for PHP
PHP
3
star
43

php-grip

Grip library for PHP
PHP
3
star
44

go-pubcontrol

Go EPCP library
Go
3
star
45

pysmartfeed

Python Smart Feed library
Python
3
star
46

rails-grip

Ruby on Rails GRIP library
Ruby
2
star
47

java-fanout

Fanout.io library for Java.
Java
2
star
48

xmpp-ftw-fanout

XMPP-FTW Fanout plugin
JavaScript
2
star
49

java-gripcontrol

A GRIP library for Java.
Java
2
star
50

go-fanout

Fanout.io library for Go.
Go
2
star
51

django-fanout

Fanout.io library for Django
Python
2
star
52

chat-demo-android

A chat demo using Fanout's APIs through event source
Java
2
star
53

vercel-websocket

JavaScript
2
star
54

django-liveresource

LiveResource library for Python/Django
Python
1
star
55

chat-demo-ios

An eventsource based chat app built to demo functionalities for Fanout
Swift
1
star
56

reliable-stream

Python
1
star
57

test-api

Minimal realtime API example
Python
1
star
58

demo

JavaScript
1
star