-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
165 lines (142 loc) · 5.9 KB
/
server.js
File metadata and controls
165 lines (142 loc) · 5.9 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require('express');
const cors = require('cors');
const path = require("path");
// helps connect to mongodb
const mongoose = require('mongoose');
const http = require('http');
const socketio = require('socket.io');
// passport is middleware for authentication
const passport = require('passport');
const {addUser, removeUser, getUser, getUsersInTeam} = require('./routes/chatUsers');
let Team = require('./models/team.model');
const app = express();
const server = http.createServer(app);
// process.env.PORT is the port that you use if you host your app somewhere, like heroku
const port = process.env.PORT || 5000;
//const server = app.listen(port)
// set express middleware
app.use(cors());
// allow us to parse json because server receive and send json
app.use(express.json());
//set up passport middleware and passport config
app.use(passport.initialize());
require('./config/passport')(passport);
// database uri from mongodb atlas dashboard
//const uri = process.env.ATLAS_URI;
const uri = require('./config/keys.js').ATLAS_URI;
// start connection with mongodb database
mongoose.connect(uri, {useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true});
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
// tell server to use these API router files (able to use API endpoints)
const teamsRouter = require('./routes/teams');
const usersRouter = require('./routes/users');
const notesRouter = require('./routes/notes');
const eventsRouter = require('./routes/events');
const tagsRouter = require('./routes/tags');
// when user goes to /teams, server will load everything in teamsRouter
app.use('/teams', teamsRouter);
app.use('/users', usersRouter);
app.use('/notes', notesRouter);
app.use('/events', eventsRouter);
app.use('/tags', tagsRouter);
// serve static files from react frontend app
app.use(express.static(path.join(__dirname, "client", "build")))
//build mode
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
})
let numMsgsRead = 5;
// web sockets for chatting
const io = socketio(server);
//const io = require("socket.io").listen(server);
io.on('connection', socket => {
// each socket instance represents a user
console.log('new connection');
// when a user joins a chat
socket.on('join', ({user, currTeam}, callback) => {
// if the team hasn't been loaded yet, just return
if (!currTeam || !currTeam._id) return;
const teams = connection.db.collection('teams');
teams.find().toArray((err, res) => {
console.log(res);
const desired = res.filter(team => team._id.toHexString() === currTeam._id)[0];
console.log('sending old msgs...');
// sends the most recent 5 messages
//socket.emit('old msgs', desired.teamChat.slice(1).slice(-5));
socket.emit('old msgs', desired.teamChat);
})
// add the user to the chat
const {err, newUser} = addUser(
{
userId: user.id,
socketId: socket.id,
name: user.firstName + " " + user.lastName,
team: currTeam
}
);
// if there was an error getting the user, call the callback
if (err) return callback(err);
/* emit welcome message to the user that just joined the chat
socket.emit('message', {user:'admin', text:`${newUser.name} welcome to ${newUser.team.teamName}`});
// tell everyone in chat besides the new user that the new user has joined
socket.broadcast.to(newUser.team.teamName).emit('message', {user:'admin', text:`${newUser.name} has joined`})
*/
// let the user join the team chat room
socket.join(newUser.team.teamName);
callback();
})
socket.on('sendMessage', (msg, callback) => {
// get the user that is sending the message
const user = getUser(socket.id);
const chatData = {user:user.name, text:msg};
// adds the new message to the given team
Team.findById(user.team._id)
.then(team => {
team.teamChat.push(chatData);
// add message to team and save to db
team.save()
// emit new message after saving the new chat msg
.then(() => io.to(user.team.teamName).emit('message', chatData))
.catch(err => console.log(err));
})
callback();
})
/*
When the user scrolls to the top and needs to load more messages,
load 5 more messages based on numMsgsRead var
*/
socket.on('loadMoreMsgs', (currTeam, callback) => {
const teams = connection.db.collection('teams');
teams.find().toArray((err, res) => {
const desired = res.filter(team => team._id.toHexString() === currTeam._id)[0];
let nextFive = null;
// if no more messages left, don't do anything
if (numMsgsRead >= desired.teamChat.length){
}
// sends the the next 5 messages (or as many messages left)
else if (numMsgsRead + 5 >= desired.teamChat.length){
nextFive = desired.teamChat.slice(numMsgsRead, desired.teamChat.length);
numMsgsRead += 5;
}
else{
nextFive = desired.teamChat.slice(numMsgsRead, numMsgsRead + 5);
numMsgsRead += 5;
}
console.log('sending next five msgs...')
callback(nextFive);
})
})
socket.on('disconnect', () => {
console.log('user has left');
const user = removeUser(socket.id);
console.log(numMsgsRead);
numMsgsRead = 5;
});
})
// starts the server (listens on certain port)
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
})