-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
54 lines (34 loc) · 1.33 KB
/
socket.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
'use strict';
const stocks = require('./models/stockValues');
const user = require('./models/userSchema');
var mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;
/*
Listens to the stocks DB using Mongo DB's changeStream feature.
When the stock price updates, it emits an event using socket, and sends the updated data to the client.
(Refer public/client.js for the frontend JS to handle this)
*/
const changeStream = stocks.watch();
module.exports = function(io) {
changeStream.on('change', (change) => {
// console.log(change.documentKey._id);
(async () => {
const result = await stocks.find(
{_id : ObjectId(change.documentKey._id)},
{ 'change.max_change':0,'change.total_change':0,stock_id:0, _id:0, __v:0}
)
//console.log(result[0].instrument);
io.emit('changeData',
result[0],
);
})();
});
io.on('connection', function (socket) {
console.log('a user connected',socket.request.session);
socket.on('disconnect', function () {
return console.log('user disconnected');
socket.removeAllListeners('disconnect');
io.removeAllListeners('connection');
});
});
};