-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
57 lines (52 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var logger = require('morgan'),
express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
pg = require('pg'),
channel_name = 'new_paid_contributions',
options = {
dotfiles: 'ignore',
etag: false,
extensions: ['htm', 'html'],
index: false,
maxAge: '1d',
redirect: false,
setHeaders: function (res, path, stat) {
res.set('x-timestamp', Date.now());
}
},
pgClient = new pg.Client(process.env.DATABASE_URL);
// APP BASIC CONFIG
app.use(express.static('public', options));
app.use(logger('short'));
app.set('view engine', 'jade');
app.set('port', (process.env.PORT || 5000));
// APP ROUTES
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!'});
});
server.listen(app.get('port'));
// POSTGRESQL CLIENT CONNECTION STUFF
pgClient.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
pgClient.on('notification', function(msg) {
if (msg.channel !== channel_name) return;
console.log('Refreshing view');
refreshQuery = "do language plpgsql $$ " +
"begin " +
"if not exists (select true from pg_stat_activity where pg_backend_pid() <> pid and query ~* 'refresh materialized .* \"1\".statistics') then " +
"refresh materialized view concurrently \"1\".statistics; " +
"end if; " +
"end; " +
"$$;";
pgClient.query(refreshQuery);
console.log('Broadcasting notification: ', msg);
io.of('/').sockets.forEach(function(socket){
socket.emit(channel_name, msg);
});
});
pgClient.query("LISTEN " + channel_name);
});