What is http long polling?
Http long polling is used in real-time application where server pushes the data changes to client using simple HTML5 ajax mechanism.
First client makes a request to an HTTP endpoint in the usual way, with the intention of requesting data it has not yet received. If there is no new data available, then the server holds the request open until data becomes available to respond with. After receiving a response (whether new data or timeout), the transaction is complete. The client may create a new request to listen for further data.
comet
The comet is a implementation of http long polling or comet application model which abstracts the internal http requests and provides high-level websocket alike API.
Installation
Using npm
:
$ npm install --save comet
Using yarn
:
$ yarn add comet
Usage
Here is simple example to push n-th fibonacci numbers to client:
The server:
;; const server = http;const pollingServer = server ; pollingServer; server; const fib = { let num = 0; let num2 = 1; let fibonacci; for let i = 0; i < n; i++ fibonacci = num + num2; num = num2; num2 = fibonacci; return num;};
The client:
; const fibPos = Math;const longPollingClient = 'http://localhost:8888/polling' data: fibPos ; longPollingClient; longPollingClient;
See example
for further guide.
Contributing
Your PRs and stars are always welcome.