-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.js
70 lines (61 loc) · 1.55 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Dependencies
const controller = require('./apiv2/controllers');
// Real Time Post functions
let clients = [];
const realTimePost = (io, socket) => {
console.log('A Client Connected');
clients.push(socket.id);
console.log(clients);
// Like Post
socket.on('/post/like', data => {
controller.likePost(data.postId, data.user._id, (err, success) => {
if(err)
socket.emit('like-err', err);
else
socket.emit('like-success', success);
})
});
// Save Post
socket.on('/post/save', data => {
controller.savePost(data.postId, data.userId, (err, success) => {
if(err)
io.emit('save-err', err);
else
io.emit('save-success', succcess);
});
});
// Comment on Post
socket.on('/post/comment', data => {
controller.commentOnPost(data.postId, data.comment, data.userId, (err, status, success) => {
if(err)
io.emit('comment-err', err);
else
io.emit('comment-success', data);
});
})
}
// Test Post function
let testClients = [];
const realTimeTest = (io, socket) => {
console.log('Test Client Connected');
testClients.push(socket.id);
console.log(testClients);
socket.on('add-post', (data) => {
controller.addTestPost(data, (err, success) => {
if(success){
io.emit('new-post', success);
}
})
});
socket.on('like-post', (id) => {
controller.likeTestPost(id, (err, success) => {
if(success){
io.emit('post-liked', success);
}
});
})
}
module.exports = {
realTimePost,
realTimeTest
};