I'm answering my own question in details:
I'd like to thanks Lellansin to point me in the right direction:
Here is my server.js
:
var express = require('express'),
debug = true,
http = require('http'),
_ = require('underscore'),
server = express(),
port = 3700,
api_host = 'api.acme.local',
io = require('socket.io').listen(server.listen(port), {log: debug});
console.log('Node server listening on port', port);
io.set('log level', 1);
server
.get('/', function(req, res){
res.send('OK');
});
var clients = {};
io.sockets.on('connection', function(socket){
console.log('Client ' + socket.id + ' is connected');
// Add the client to the list of connected clients
clients[socket.id] = true;
// Broadcast to everyone the list of connected clients
io.sockets.emit('connected_clients', _.size(clients));
// Send the current positions to the connected client when client is ready
socket.on('ready', function() {
getCourses(function(data){
console.log('Send locations to client ' + socket.id);
socket.emit('update_locations', data);
});
});
// When a client is disconnecting, inform other clients
socket.on('disconnect', function() {
delete clients[socket.id];
console.log('Client "' + socket.id + '" disconnected');
io.sockets.emit('connected_clients', _.size(clients));
});
});
// Update locations every minutes
setInterval(function()
{
// Do nothing if there is no client
if (_.size(clients) == 0) {
return;
}
console.log('Update positions...');
// Get the current courses and broadcast them to all clients
getCourses(function(data){
io.sockets.emit('update_locations', data);
});
}, 60000); // every minute
// Update the server date every seconds
setInterval(function(){
io.sockets.emit('date', {'date': new Date()});
}, 1000);
function getCourses(callback)
{
var options = {
hostname: api_host,
port: 80,
path: '/courses',
method: 'GET'
};
var req = http.request(options, function(res) {
//console.log('-----------------------------------------');
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
var output = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function() {
var obj = JSON.parse(output);
if (callback != undefined){
callback(obj);
}
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
I only broadcast the data if there is at least one client connected. I can't why it would not be a good idea but I'll appreciate any critics. Also it seems to me that it would start to be difficult to read if I'll have multiple API request on the server side.