-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (42 loc) · 1.43 KB
/
app.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
const express = require("express");
const port = 80;
const app = express();
const path = require("path");
const publicDir = path.join(__dirname, "public");
const Pusher = require("pusher");
const pusher = new Pusher({
appId: '1048997',
key: 'e2bbfffc0a96b48eb301',
secret: '4924cbb738e5d93ffc79',
cluster: 'eu',
useTLS: true
});
const uri = "mongodb+srv://alex:[email protected]/?retryWrites=true&w=majority";
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient(uri, { useUnifiedTopology: true });
async function monitorListingsUsingEventEmitter (client, pipeline = []) {
console.log('monitorListingsUsingEventEmitter... ');
const collection = client.db("test").collection("notes");
const changeStream = collection.watch(pipeline);
changeStream.on('change', (next) => {
console.log(next);
pusher.trigger('private-my-channel', 'my-event', { "message": "hello world private" });
});
}
async function run () {
console.log('trying to connect to mongo client...');
await client.connect()
.then(() => {
console.log('CONNECTED TO MONGODB');
})
.catch((err) => {
console.log(err);
})
monitorListingsUsingEventEmitter(client)
}
console.log('calling run function.... ');
run()
app.use(express.static(publicDir));
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});