Created
December 26, 2014 04:25
-
-
Save huttj/862467833f6d4d7b10a0 to your computer and use it in GitHub Desktop.
Simple implementation of the 'heartbeat' pattern in NodeJS.
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
<!doctype html> | |
<html> | |
<head> | |
<title>Heartbeat test.</title> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
</head> | |
<body> | |
<script> | |
// Let the user set the ID in the query variable | |
var id = parseInt((window.location.search.match(/id=(\d{2})/) || []) [1]); | |
// If the user did not set the ID, choose one at random and set it | |
// as the query variable (for easy copy-and-paste) | |
if (!id) { | |
id = Math.round(Math.random()*100), 10 | |
window.location.search = "?id=" + id; | |
} | |
// Show the current client ID | |
$('body').append($('<h1></h1>').text('Your ID: ' id)); | |
// Show a list of the connected clients | |
var clients = $('<p class="clients"></p>'); | |
$('body').append(clients); | |
// Poll the server with a heartbeat request every second. | |
var heartbeat = setInterval(function() { | |
$.get('http://localhost:9001/heartbeat?id=' + id, function(data) { | |
var d = JSON.parse(data); | |
clients.text(d.join(', ') + ' (' + d.length + ' clients)'); | |
console.log(data); | |
}); | |
}, 1000); | |
</script> | |
</body> | |
</html> |
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 http = require('http'); | |
var fs = require('fs'); | |
// Simple client page that sends a "heartbeat" GET request every second | |
var index = fs.readFileSync('index.html'); | |
// List of clients' setTimeout IDs keyed on client-provided 'ID' | |
// Represents 'active' clients | |
var clients = {}; | |
// Single request | |
http.createServer(function(req, res) { | |
console.log(req.url); | |
// If the word 'heartbeat' is present, get the ID associated with the | |
// request and reset its self-removal timeout--that is, give it another | |
// Five seconds to check back in before removing it from the list | |
if (req.url.match(/heartbeat/)) { | |
// Get the ID from the request header | |
var id = req.url.match(/id=(\d+)/)[1]; | |
// Remove the timeout that would remove the ID from the clients list | |
clearTimeout(clients[id]); | |
// Set a new timeout to remove the client if they haven't checked in | |
// within five seconds from now | |
clients[id] = setTimeout((function(_id) { | |
// Capture the ID in a local variable so it's accessible in this scope | |
return function() { | |
console.log('deleting', _id); | |
delete clients[_id]; | |
} | |
}(id)), 5000); | |
// Send out a list of the connected clients the the client | |
var clientList = []; | |
for (var prop in clients) { | |
clientList.push(prop); | |
} | |
res.end(JSON.stringify(clientList)); | |
} else { | |
// If 'heartbeat' is not in the url, send a copy of the index file. | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.end(index); | |
} | |
}).listen(9001); | |
console.log('Listening on port ' + 9001); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment