2020-12-17 18:52:06 -05:00
// Generated by CoffeeScript 2.5.1
2011-11-06 12:21:27 -08:00
( function ( ) {
2021-01-05 19:34:28 -05:00
var DEV , DOCKER , REDIS _PORT , REDIS _SERVER , SECURE , app , certificate , fs , io , log , options , privateKey , redis ;
2011-11-06 12:21:27 -08:00
fs = require ( 'fs' ) ;
redis = require ( 'redis' ) ;
2013-05-01 12:51:59 -07:00
log = require ( './log.js' ) ;
2019-04-13 15:28:56 -04:00
DEV = process . env . NODE _ENV === 'development' ;
2020-10-05 00:34:40 +07:00
DOCKER = process . env . NODE _ENV === 'docker' ;
2012-03-28 19:13:30 -07:00
2020-10-05 00:34:40 +07:00
REDIS _SERVER = process . env . NODE _ENV === 'development' ? 'localhost' : DOCKER ? 'redis' : 'db_redis_pubsub' ;
2021-01-05 19:34:28 -05:00
2012-12-24 11:53:00 -08:00
SECURE = ! ! process . env . NODE _SSL ;
2020-10-26 10:36:24 +07:00
REDIS _PORT = DOCKER ? 6579 : 6379 ;
2021-01-05 19:34:28 -05:00
// client = redis.createClient 6379, REDIS_SERVER
// RedisStore = require 'socket.io/lib/stores/redis'
// rpub = redis.createClient 6379, REDIS_SERVER
// rsub = redis.createClient 6379, REDIS_SERVER
// rclient = redis.createClient 6379, REDIS_SERVER
2019-04-13 15:28:56 -04:00
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" ) ;
}
2012-12-24 11:53:00 -08:00
if ( SECURE ) {
2016-11-29 18:47:44 -08:00
privateKey = fs . readFileSync ( '/srv/newsblur/config/certificates/newsblur.com.key' ) . toString ( ) ;
certificate = fs . readFileSync ( '/srv/newsblur/config/certificates/newsblur.com.crt' ) . toString ( ) ;
2020-12-17 18:52:06 -05:00
// ca = fs.readFileSync('./config/certificates/intermediate.crt').toString()
2016-02-05 15:26:10 -08:00
options = {
2016-11-29 18:30:12 -08:00
port : 8889 ,
2012-12-24 11:53:00 -08:00
key : privateKey ,
2015-06-02 11:24:38 -07:00
cert : certificate
2016-02-05 15:26:10 -08:00
} ;
2016-11-29 18:47:44 -08:00
app = require ( 'https' ) . createServer ( options ) ;
2016-11-30 13:43:13 -08:00
io = require ( 'socket.io' ) ( app , {
2021-01-04 18:14:52 -05:00
path : "/v3/socket.io"
2016-11-30 15:26:42 -08:00
} ) ;
app . listen ( options . port ) ;
2020-12-17 18:52:06 -05:00
log . debug ( ` Listening securely on port ${ options . port } ` ) ;
2012-12-24 11:53:00 -08:00
} else {
2016-11-29 18:47:44 -08:00
options = {
port : 8888
} ;
app = require ( 'http' ) . createServer ( ) ;
2016-11-30 13:43:13 -08:00
io = require ( 'socket.io' ) ( app , {
2021-01-04 18:14:52 -05:00
path : "/v3/socket.io"
2016-11-30 15:26:42 -08:00
} ) ;
app . listen ( options . port ) ;
2020-12-17 18:52:06 -05:00
log . debug ( ` Listening on port ${ options . port } ` ) ;
2012-12-24 11:53:00 -08:00
}
2020-12-17 18:52:06 -05:00
// io.set('transports', ['websocket'])
// io.set 'store', new RedisStore
// redisPub : rpub
// redisSub : rsub
// redisClient : rclient
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 ;
2020-12-17 18:59:49 -05:00
socket . on ( 'subscribe:feeds' , ( feeds , username ) => {
2020-12-17 18:52:06 -05:00
var ref ;
2012-04-03 22:01:12 -07:00
this . feeds = feeds ;
this . username = username ;
2020-12-17 18:52:06 -05:00
log . info ( this . username , ` Connecting ( ${ this . feeds . length } feeds, ${ ip } ), ` + ` ( ${ io . engine . clientsCount } connected) ` + ` ${ SECURE ? "(SSL)" : "(non-SSL)" } ` ) ;
2013-04-08 10:23:26 -07:00
if ( ! this . username ) {
return ;
}
2013-08-12 11:52:29 -07:00
socket . on ( "error" , function ( err ) {
2020-12-17 18:52:06 -05:00
return log . debug ( ` Error (socket): ${ err } ` ) ;
2013-08-12 11:52:29 -07:00
} ) ;
2020-12-17 18:52:06 -05:00
if ( ( ref = socket . subscribe ) != null ) {
ref . quit ( ) ;
2012-10-22 16:25:36 -07:00
}
2020-10-26 10:36:24 +07:00
socket . subscribe = redis . createClient ( REDIS _PORT , REDIS _SERVER ) ;
2021-01-05 19:34:28 -05:00
socket . subscribe . on ( "error" , ( err ) => {
var ref1 ;
log . info ( this . username , ` Error: ${ err } ( ${ this . feeds . length } feeds) ` ) ;
return ( ref1 = socket . subscribe ) != null ? ref1 . quit ( ) : void 0 ;
} ) ;
socket . subscribe . on ( "connect" , ( ) => {
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 ) ;
} ) ;
return socket . subscribe . on ( 'message' , ( 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 ) ;
}
} ) ;
2011-11-06 12:21:27 -08:00
} ) ;
2020-12-17 18:59:49 -05:00
return socket . on ( 'disconnect' , ( ) => {
2020-12-17 18:52:06 -05:00
var ref , ref1 ;
if ( ( ref = socket . subscribe ) != null ) {
ref . quit ( ) ;
2012-10-22 16:25:36 -07:00
}
2020-12-17 18:52:06 -05: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)" } ` ) ;
2011-11-06 12:21:27 -08:00
} ) ;
} ) ;
2013-08-12 11:52:29 -07:00
io . sockets . on ( 'error' , function ( err ) {
2020-12-17 18:52:06 -05:00
return log . debug ( ` Error (sockets): ${ err } ` ) ;
2013-08-12 11:52:29 -07:00
} ) ;
2011-11-06 12:21:27 -08:00
} ) . call ( this ) ;