-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (54 loc) · 1.79 KB
/
server.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
const mongo = require('mongodb').MongoClient;
const client = require('socket.io').listen(4000).sockets;
// Connect to mongo
mongo.connect('mongodb://127.0.0.1/mean-socketchat', (err, db) => {
if (err){
throw err;
}
console.log('MongoDB connected');
// Connect to socket.io
client.on('connection', (socket) => {
//creating a collection
const myDB = db.db('mean-socketchat') // for higher version of mongo!
let chat = myDB.collection('chats');
// creating function to send status
sendStatus = (s) => {
socket.emit('status', s);
}
// Get chats from mongo collection
chat.find().limit(100).sort({_id:1}).toArray( (err, res) => {
if (err){
throw err;
}
// Emit the messages
socket.emit('output', res);
});
// Handle input events
socket.on('input', (data) => {
let name = data.name;
let message = data.message;
// Check for name and message
if (name == '' || message == ''){
sendStatus('Please enter a name and a message');
}
else {
// Insert Message
chat.insert({name: name, message: message}, () => {
client.emit('output', [data]);
// Send status object
sendStatus({
message: 'Message sent',
clear: true
});
});
}
});
// Handle clear
socket.on('clear', (data) => {
// Remove all chats from collection
chat.remove({}, () => {
socket.emit('cleared');
});
});
});
});