NewsBlur/node/unread_counts.js

118 lines
4.1 KiB
JavaScript
Raw Normal View History

// Generated by CoffeeScript 1.8.0
(function() {
var DEV, REDIS_SERVER, SECURE, app, certificate, fs, io, log, options, privateKey, redis;
fs = require('fs');
redis = require('redis');
2013-05-01 12:51:59 -07:00
log = require('./log.js');
DEV = process.env.NODE_ENV === 'development';
2013-08-12 12:39:03 -07:00
REDIS_SERVER = process.env.NODE_ENV === 'development' ? 'localhost' : 'db_redis_pubsub';
SECURE = !!process.env.NODE_SSL;
log.debug("Starting NewsBlur unread count server...");
if (!DEV && !process.env.NODE_ENV) {
log.debug("Specify NODE_ENV=<development,production>");
return;
} else if (DEV) {
log.debug("Running as development server");
} else {
log.debug("Running as production server");
}
if (SECURE) {
privateKey = fs.readFileSync('/srv/newsblur/config/certificates/newsblur.com.key').toString();
certificate = fs.readFileSync('/srv/newsblur/config/certificates/newsblur.com.crt').toString();
options = {
2016-11-29 18:30:12 -08:00
port: 8889,
key: privateKey,
2015-06-02 11:24:38 -07:00
cert: certificate
};
app = require('https').createServer(options);
io = require('socket.io')(app, {
path: "/v2/socket.io"
});
app.listen(options.port);
log.debug("Listening securely on port " + options.port);
} else {
options = {
port: 8888
};
app = require('http').createServer();
io = require('socket.io')(app, {
path: "/v2/socket.io"
});
app.listen(options.port);
log.debug("Listening on port " + options.port);
}
2016-11-29 18:30:12 -08:00
io.on('connection', function(socket) {
2013-05-01 13:22:03 -07:00
var ip;
2016-11-29 18:30:12 -08:00
ip = socket.handshake.headers['X-Forwarded-For'] || socket.handshake.address;
socket.on('subscribe:feeds', function(feeds, username) {
var _ref;
this.feeds = feeds;
this.username = username;
2016-11-29 18:30:12 -08:00
log.info(this.username, ("Connecting (" + feeds.length + " feeds, " + ip + "),") + (" (" + io.engine.clientsCount + " connected) ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)")));
if (!this.username) {
return;
}
socket.on("error", function(err) {
return log.debug("Error (socket): " + err);
});
if ((_ref = socket.subscribe) != null) {
_ref.quit();
}
socket.subscribe = redis.createClient(6379, REDIS_SERVER);
2016-12-05 14:18:48 -08:00
socket.subscribe.on("error", (function(_this) {
return function(err) {
var _ref1;
log.info(_this.username, "Error: " + err + " (" + _this.feeds.length + " feeds)");
return (_ref1 = socket.subscribe) != null ? _ref1.quit() : void 0;
};
})(this));
socket.subscribe.on("connect", (function(_this) {
return function() {
var feeds_story;
log.info(_this.username, ("Connected (" + _this.feeds.length + " feeds, " + ip + "),") + (" (" + io.engine.clientsCount + " connected) ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)")));
socket.subscribe.subscribe(_this.feeds);
feeds_story = _this.feeds.map(function(f) {
return "" + f + ":story";
});
socket.subscribe.subscribe(feeds_story);
return socket.subscribe.subscribe(_this.username);
};
})(this));
return socket.subscribe.on('message', (function(_this) {
return function(channel, message) {
log.info(_this.username, "Update on " + channel + ": " + message);
if (channel === _this.username) {
return socket.emit('user:update', channel, message);
} else if (channel.indexOf(':story') >= 0) {
return socket.emit('feed:story:new', channel, message);
} else {
return socket.emit('feed:update', channel, message);
}
};
})(this));
});
return socket.on('disconnect', function() {
2013-05-01 13:22:03 -07:00
var _ref, _ref1;
if ((_ref = socket.subscribe) != null) {
_ref.quit();
}
2016-11-29 18:30:12 -08:00
return log.info(this.username, ("Disconnect (" + ((_ref1 = this.feeds) != null ? _ref1.length : void 0) + " feeds, " + ip + "),") + (" there are now " + io.engine.clientsCount + " users. ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)")));
});
});
io.sockets.on('error', function(err) {
return log.debug("Error (sockets): " + err);
});
}).call(this);