-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
46 lines (28 loc) · 1.31 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
const { removePost, addPost, addPostComment } = require('./utils/postsHandler');
const { addUser, removeUser} = require('./utils/usersHandler');
module.exports = (io) => io.on('connect', (socket) => {
socket.on('join', ({ subID }) => {
let loggedUsers = addUser(subID);
io.emit('online', { onlineUsers : loggedUsers });
});
socket.on('logout', ({ subID, users }) => {
let onlineUsers = removeUser(subID, users);
//console.log(onlineUsers);
io.emit('userLoggedOut', { onlineUsers : onlineUsers });
})
socket.on('createPost', async ({ postMessage, sub, picture, name }, callback) => {
let newPost = await addPost({ postMessage,sub,picture,name });
await io.emit('newPost', { newPost : newPost });
callback();
})
socket.on('removePost', async ({ id, authorId }) => {
let removedPostId = await removePost({ id, authorId });
io.emit('removedPostId', { removedPostId : removedPostId });
});
socket.on('postComment', async ( {id, comment, sub, picture, name}) => {
let updatedPost = await addPostComment({ id, comment, sub, picture, name });
io.emit('updatedPostWithComments', { post : updatedPost })
})
socket.on('disconnect', () => {
});
});