-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (42 loc) · 1.25 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
const express = require('express');
const bodyParser = require('body-parser');
const Pusher = require('pusher');
const pg = require ('pg');
require('dotenv').config();
const app = express();
let pgClient;
// set the view engine to ejs
app.set('view engine', 'ejs');
const pool = new pg.Pool({
connectionString: process.env.POSTGRES_CONNECTION_URL,
});
// Body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Session middleware
// Create an instance of Pusher
const pusher = new Pusher({
appId: process.env.PUSHER_APP_ID,
key: process.env.PUSHER_APP_KEY,
secret: process.env.PUSHER_APP_SECRET,
cluster: process.env.PUSHER_APP_CLUSTER,
encrypted: true
});
pool.connect((err, client) => {
if(err) {
console.log(err);
}
pgClient = client;
client.on('notification', function(msg) {
pusher.trigger('watch_realtime_table', 'new_record', JSON.parse(msg.payload));
});
const query = client.query('LISTEN watch_realtime_table');
});
app.get('/', async(req, res) => {
const data = await pgClient.query('SELECT * FROM realtime_table');
return res.render('index', {table: data.rows});
});
//listen on the app
app.listen(3000, () => {
return console.log('Server is up on 3000')
});