Skip to content

Instantly share code, notes, and snippets.

@auser
Last active March 9, 2020 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save auser/7997500 to your computer and use it in GitHub Desktop.
Save auser/7997500 to your computer and use it in GitHub Desktop.
var Pusher = require('pusher'),
os = require('os'),
express = require('express'),
app = express(),
ch = 'stats';
var pusher = new Pusher({
appId: process.env.PUSHER_APP_ID,
key: process.env.PUSHER_KEY,
secret: process.env.PUSHER_SECRET
});
var pushStatistics = function(cb) {
// Gather stats
var load = os.loadavg(),
totmem = os.totalmem(),
freemem = os.freemem();
pusher.trigger(ch, 'stats', {
precentfreemem: (freemem/totmem).toFixed(3),
load5: load[1].toFixed(3),
node: os.hostname()
});
cb();
}
var statTimeout,
setPushStatsTimer = function() {
pushStatistics(function() {
statTimeout = setTimeout(setPushStatsTimer, 5 * 1000);
});
}
app.get('/start', function(req, res) {
setPushStatsTimer();
res.send('ok');
})
app.get('/stop', function(req, res) {
clearTimeout(statTimeout);
res.send('ok');
})
app.listen(process.env.PORT || 3000)
{
"name": "pusherexample",
"version": "0.0.1",
"description": "",
"main": "index.js",
"author": "Ari Lerner <ari@fullstack.io>",
"dependencies": {
"pusher": "~0.1.3",
"express": "~3.4.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment