-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (42 loc) · 1.63 KB
/
server.js
File metadata and controls
50 lines (42 loc) · 1.63 KB
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
var server = require('http').createServer(),
io = require('socket.io')(server),
jwt = require('jsonwebtoken'),
port = 3000;
var token_key = "YOUR_JWT_SECRET_KEY";
io.use(function (socket, next) {
if (socket.handshake.query && socket.handshake.query.token) {
jwt.verify(socket.handshake.query.token, token_key, function (err, decoded) {
if (err) return next(new Error('Authentication error'));
socket.decoded = decoded;
socket.jwtToken = socket.handshake.query.token;
socket.member_id = socket.decoded.sub;
next();
});
} else {
next(new Error('Authentication error'));
}
})
.on('connection', function (socket) {
console.log(socket.member_id);
// Join User Room(Channel)
socket.join('member_' + socket.member_id);
/*
* Listen to events
*/
// Send new message
socket.on('sendMessage', function (message) {
message.from_member_id = socket.member_id;
// console.log(message);
// Save Message History code
// HERE YOU CAN STORE MESSAGES HISTORY WITH YOUR DB
// Send message to two members
const channels = ['member_' + message.member_id, 'member_' + message.from_member_id];
channels.forEach((roomID) => {
console.log(roomID);
if (io.sockets.adapter.rooms[roomID].length > 0) {
io.sockets.in(roomID).emit('receiveMessage', message)
}
});
});
});
server.listen(port);