title: Broadcast GET data from API to clients using socket.io
body: <p>I'm not sure that what I'm trying to achieve is the right way to do it because I cannot find any example.</p>
  
  <p>I have an application which is displaying a Google map and multiple objects on it. I want the location of the objects to be updated automatically every seconds or 2.</p>
  
  <p>I was thinking of having a nodejs server with a <code>setInterval()</code> that would be triggered every 2 seconds to perform an API request to a different server, get the data response and broadcast it to all socket.io clients.</p>
  
  <p>Here is my <code>server.js</code> that I'm using at the moment:</p>
  
  <pre><code>var express = require('express'),
      server = express(),
      port = 3700,
      host = 'localhost',
      io = require('socket.io').listen(server.listen(port, host));
  
  server
      .get('/', function(req, res){
          res.send('OK');
      });
  
  io.sockets.on('connection', function(socket){
      console.log('connection started');
  
      // Update the server date every seconds
      setInterval(function(){
          socket.emit('date', {'date': new Date()});
      }, 1000);
  
      // Update locations every minutes
      setInterval(function(){
          console.log('Client: ' + socket.id);
          io.sockets.emit('update_locations', []);
      }, 1000);
  });
  
  io.sockets.on('disconnect', function(socket){
      console.log('Client "' + socket.id + '" disconnected');
  });
  </code></pre>
  
  <p>See where I was looking to broadcast the <code>update_locations</code> message. 
  I was investigating how I should perform the request to my API?</p>
  
  <ul>
  <li>Am I doing it the right way? </li>
  <li>Should I use <code>http.get()</code> in my <code>setInterval()</code> function?</li>
  <li>Should I use <code>setInterval()</code>? I can't see how I would be able to do it without <code>setInterval()</code></li>
  </ul>
  
  <p>Cheer, 
  Maxime</p>