var request = require('request');
var fetchfeed = require('./fetchfeed.js');
var nxws = new fetchfeed();
var feeds = require('./feeds.json').remote;
var checkNewsInterval;

var db = require('monk')('localhost/newsdb');
var db_items = db.get('items');

function fetchFeeds() {
  // console.log('Fetching', feeds.length, 'feeds');
  nxws.fetchSourceFromStream(feeds, request, storeContentsOfFeed);
}

fetchFeeds();
setInterval(function() {
  fetchFeeds();
}, 10000);

/* Storage */
function storeContentsOfFeed(err, x) {  
  if (err) {
    console.error(err);
    return;
  }
  
  var newItem = {
    title: x.title,
    author: x.author,
    date: new Date(x.date),
    guid: x.guid,
    link: x.link,
    metatitle: x.meta.title,
    metalink: x.meta.link
  }
  if (newItem.date > new Date()) newItem.date = new Date();  
  
  db_items.findOne({guid: newItem.guid}, function(e, d){
    if (err) throw err;
    if (null == d) {
      console.log("(" + new Date() + ") inserting and sending: ", newItem.title);
      db_items.insert(newItem, function(err, d) {
        if (err) throw err;
        io.emit('nxws items', JSON.stringify([d]));
      });
      
    }
  });
}

/* Server */
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));

http.listen(3000, function() {
	console.log('listening on *:3000');
});

/* Socket */
io.on('connection', function(socket) {
	console.log("User connected", socket.id);
  emitNumberOfUsers(io.sockets.sockets.length);
  socket.beginTime = new Date();
  
  socket.on('request news', function() {
    sendAllNews(socket);
  });
  
  socket.on('disconnect', function() {
  	console.log("User disconnected", socket.id);
    emitNumberOfUsers(io.sockets.sockets.length);
  });    
});

/* Broadcasting */
function sendAllNews(socket) {
  db_items.find({}, {sort: {date: -1}})
          .on('success', createNewsEmitterForSocket(socket));
}

function emitNumberOfUsers(num) {
  console.log('User count', io.sockets.sockets.length)
  io.emit('nxws readers', num);
}

function createNewsEmitterForSocket(socket) {
  return function (docs) {
    console.log('Sending ' + docs.length + ' items to ' + socket.id);
    socket.emit('nxws items', JSON.stringify(docs));
  }
}

@@ -239,15 +239,47 @@
 et('
-items')
+rss_guids');%0A%0Avar REFRESH_DELAY = 30000
 ;%0A%0Af
@@ -486,13 +486,21 @@
 %0A%7D, 
-10000
+REFRESH_DELAY
 );%0A%0A
@@ -792,16 +792,17 @@
 link%0A  %7D
+;
 %0A  if (n
@@ -955,16 +955,17 @@
 (null ==
+=
  d) %7B%0A  
@@ -1066,23 +1066,36 @@
 .insert(
+%7Bguid: 
 newItem
+.guid%7D
 , functi
@@ -1181,17 +1181,23 @@
 ingify(%5B
-d
+newItem
 %5D));%0A   
@@ -2141,16 +2141,17 @@
 .length)
+;
 %0A  io.em
@@ -2377,11 +2377,12 @@
 s));%0A  %7D
+;
 %0A%7D%0A

Add refresh delay of 30 seconds.