Last active
March 9, 2020 14:27
-
-
Save auser/7997500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "pusherexample", | |
"version": "0.0.1", | |
"description": "", | |
"main": "index.js", | |
"author": "Ari Lerner <[email protected]>", | |
"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