{"size": 1381, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "dvrylc/twitter-trends-server", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "dvrylc/twitter-trends-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "dvrylc/twitter-trends-server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.1090909091, "max_line_length": 99, "alphanum_fraction": 0.58435916}
// External imports
const app = require('http').createServer(handler);
const io  = require('socket.io')(app);

// Internal imports
const api = require('./api');

// Request handler
function handler (req, res) {
  res.end();
}

// Connect to Twitter API
api.stream('statuses/sample', stream => {
  // Start socket.io server
  io.on('connection', socket => {
    const clientIP = socket.client.request.headers['x-forwarded-for'] || socket.conn.remoteAddress;
    console.log(`[CONNECT]\t\t${clientIP}`);
    
    // Set unlimited clients
    socket.setMaxListeners(0);
    
    // Emit incoming tweets
    stream.on('data', tweet => {
      // Ignore empty tweets and retweets
      if (tweet.text && !tweet.retweeted_status) {
        const text = tweet.text;
        const hashtags = tweet.entities.hashtags.map(h => h.text);
        const url = `https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`;
        
        socket.emit('tweet', {
          text,
          hashtags,
          url
        });
      }
    });
    
    // Emit errors
    stream.on('error', err => {
      socket.emit('error', err);
    });
    
    // Handle disconnect
    socket.on('disconnect', () => {
      console.log(`[DISCONNECT]\t${clientIP}`);
    });
  });
});

// Bind server to port
app.listen(process.env.PORT || 8000, () => {
  console.log('[INFO]\t\tServer started');
});
