
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
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.
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.
Using npm
:
$ npm install --save comet
Using yarn
:
$ yarn add comet
Here is simple example to push n-th fibonacci numbers to client:
The server:
import http from 'http';
import CometServer from 'comet/server';
const server = http.createServer();
const pollingServer = new CometServer({ server });
pollingServer.on('connection', (conn, data) => {
let { fibPos } = data;
console.log(`New connection with fibpos: ${fibPos}`);
setInterval(() => {
conn.send({ pos: fibPos, value: fib(fibPos++) });
}, 1000);
});
server.listen(8888);
const fib = (n) => {
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:
import CometClient from 'comet/client';
const fibPos = Math.floor(Math.random() * 10);
const longPollingClient = new CometClient('http://localhost:8888/polling', {
data: { fibPos },
});
longPollingClient.on('message', (m) => {
console.log(`${m.pos}th: ${m.value}`);
});
longPollingClient.on('open', (conn) => {
console.log('Connection opened');
console.log(`Receiving fibonacci numbers from ${fibPos}th position:`);
});
See example
for further guide.
Your PRs and stars are always welcome.
FAQs
A http long polling comet implementation for nodejs and browser
The npm package comet receives a total of 1 weekly downloads. As such, comet popularity was classified as not popular.
We found that comet demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.