Skip to content

Commit fe06b46

Browse files
committed
designed and developed chat application
1 parent 3c60baa commit fe06b46

File tree

204 files changed

+63974
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+63974
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
# reactjs-express-nodejs-mongo-socket.io-Community_chat_with_database_saving_and_private_chat_wo_db_sa
2+
23
reactjs-express-nodejs-mongo-socket.io-Community_chat_with_database_saving_and_private_chat_without_db_saving
4+
5+
#My Server Chat is the chatting based application where user can do chatting in community by pick up unique name and proceed to the app and user can also see the online users where they can do private messaging to each others.
6+
7+
#start client and server
8+
9+
> npm run dev
10+
11+
#local mongodb url: mongodb://localhost/mychatserver
12+
13+
#client run on : http://localhost:3000/
14+
15+
#node and socket.io server run on : http://localhost:5005/

SocketManager.js

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
const bcrypt = require("bcryptjs");
4+
const jwt = require("jsonwebtoken");
5+
var fs = require("fs");
6+
const passport = require("passport");
7+
8+
var dateFormat = require("dateformat");
9+
10+
const nodemailer = require("nodemailer");
11+
12+
const multer = require("multer");
13+
const path = require("path");
14+
15+
// Load User model
16+
const User = require("./models/User");
17+
// Load Matcheduserlist model
18+
const Matcheduserlist = require("./models/Matcheduserlist");
19+
// Load Communitychats model
20+
const Communitychats = require("./models/Communitychats");
21+
22+
const io = require("./mychatserver.js").io;
23+
24+
const {
25+
VERIFY_USER,
26+
USER_CONNECTED,
27+
USER_DISCONNECTED,
28+
LOGOUT,
29+
COMMUNITY_CHAT,
30+
COMMUNITY_CHAT_HISTORY,
31+
MESSAGE_RECIEVED,
32+
MESSAGE_SENT,
33+
TYPING,
34+
PRIVATE_MESSAGE
35+
} = require("./client/src/Events");
36+
37+
const {
38+
createUser,
39+
createMessage,
40+
createChat
41+
} = require("./client/src/Factories");
42+
43+
let connectedUsers = {};
44+
45+
let communityChat = createChat({ isCommunity: true });
46+
47+
module.exports = function(socket) {
48+
// console.log('\x1bc'); //clears console
49+
50+
console.log("New user connected where Socket Id:" + socket.id);
51+
52+
let sendMessageToChatFromUser;
53+
54+
let sendTypingFromUser;
55+
56+
//listen on VERIFY_USER
57+
//Verify Username
58+
socket.on(VERIFY_USER, (nickname, callback) => {
59+
console.log("listen on VERIFY_USER");
60+
if (isUser(connectedUsers, nickname)) {
61+
callback({ isUser: true, user: null });
62+
} else {
63+
callback({
64+
isUser: false,
65+
user: createUser({ name: nickname, socketId: socket.id })
66+
});
67+
}
68+
});
69+
70+
//In order to send an event to everyone, Socket.IO gives us the io.emit
71+
72+
//listen on USER_CONNECTED
73+
//User Connects with username
74+
socket.on(USER_CONNECTED, user => {
75+
console.log("listen on USER_CONNECTED");
76+
user.socketId = socket.id;
77+
connectedUsers = addUser(connectedUsers, user);
78+
socket.user = user;
79+
80+
sendMessageToChatFromUser = sendMessageToChat(user.name);
81+
sendTypingFromUser = sendTypingToChat(user.name);
82+
83+
//it will broadcast the new user connected list to all the connected users from our socket server in case always when new user is connected
84+
85+
//thats why in the section of connected user left tab users list is always up to date
86+
io.emit(USER_CONNECTED, connectedUsers);
87+
console.log(connectedUsers);
88+
});
89+
90+
socket.on(COMMUNITY_CHAT_HISTORY, callback => {
91+
console.log("listen on COMMUNITY_CHAT_HISTORY");
92+
93+
Communitychats.find({}).then(communitychats => {
94+
//res.json(communitychats);
95+
console.log("communitychats history found!!");
96+
callback(communitychats);
97+
});
98+
});
99+
100+
socket.on(COMMUNITY_CHAT, callback => {
101+
console.log("listen on COMMUNITY_CHAT");
102+
console.log(communityChat);
103+
callback(communityChat);
104+
});
105+
106+
//Each socket also fires a special disconnect event
107+
//User disconnects
108+
socket.on("disconnect", () => {
109+
console.log("listen on disconnect");
110+
if ("user" in socket) {
111+
console.log(
112+
"disconnected user is remove from our socket connectedUsers object list"
113+
);
114+
115+
connectedUsers = removeUser(connectedUsers, socket.user.name);
116+
117+
console.log("here we broadcast final connected users list");
118+
119+
io.emit(USER_DISCONNECTED, connectedUsers);
120+
console.log("Final Connected Users are : ", connectedUsers);
121+
}
122+
});
123+
124+
//listen on LOGOUT
125+
//User logout
126+
socket.on(LOGOUT, () => {
127+
console.log("listen on LOGOUT");
128+
129+
connectedUsers = removeUser(connectedUsers, socket.user.name);
130+
io.emit(USER_DISCONNECTED, connectedUsers);
131+
console.log("Disconnect", connectedUsers);
132+
});
133+
134+
//Get Community Chat
135+
socket.on(COMMUNITY_CHAT, callback => {
136+
console.log("listen on COMMUNITY_CHAT");
137+
console.log(communityChat);
138+
callback(communityChat);
139+
});
140+
141+
//listen on MESSAGE_SENT
142+
socket.on(MESSAGE_SENT, ({ chatId, message }) => {
143+
console.log("listen on MESSAGE_SENT");
144+
console.log("chatid is : " + chatId + " where message is : " + message);
145+
146+
sendMessageToChatFromUser(chatId, message);
147+
});
148+
149+
socket.on(TYPING, ({ chatId, isTyping }) => {
150+
console.log("listen on TYPING");
151+
152+
sendTypingFromUser(chatId, isTyping);
153+
});
154+
155+
socket.on(PRIVATE_MESSAGE, ({ reciever, sender, activeChat }) => {
156+
console.log("listen on PRIVATE_MESSAGE");
157+
if (reciever in connectedUsers) {
158+
console.log("reciever is : " + reciever + " and sender is : " + sender);
159+
160+
const recieverSocket = connectedUsers[reciever].socketId;
161+
162+
console.log("recieverSocket is : " + recieverSocket);
163+
164+
if (activeChat === null || activeChat.id === communityChat.id) {
165+
console.log(
166+
"activeChat === null || activeChat.id === communityChat.id"
167+
);
168+
169+
const newChat = createChat({
170+
name: `${reciever}&${sender}`,
171+
users: [reciever, sender]
172+
});
173+
174+
// sending to individual socketid (private message)
175+
//eg: socket.to(socket.id).emit('hey', 'I just met you');
176+
177+
//newChat means freash chats of two private users
178+
179+
console.log("private fresh chat is : " + newChat.id);
180+
181+
socket.to(recieverSocket).emit(PRIVATE_MESSAGE, newChat);
182+
socket.emit(PRIVATE_MESSAGE, newChat);
183+
} else {
184+
//activeChat means prev chats of two private users
185+
console.log("activeChat || activeChat.id != communityChat.id");
186+
187+
socket.to(recieverSocket).emit(PRIVATE_MESSAGE, activeChat);
188+
}
189+
}
190+
});
191+
};
192+
/*
193+
* Returns a function that will take a chat id and a boolean isTyping
194+
* and then emit a broadcast to the chat id that the sender is typing
195+
* @param sender {string} username of sender
196+
* @return function(chatId, message)
197+
*/
198+
function sendTypingToChat(user) {
199+
return (chatId, isTyping) => {
200+
io.emit(`${TYPING}-${chatId}`, { user, isTyping });
201+
};
202+
}
203+
204+
/*
205+
* Returns a function that will take a chat id and message
206+
* and then emit a broadcast to the chat id.
207+
* @param sender {string} username of sender
208+
* @return function(chatId, message)
209+
*/
210+
function sendMessageToChat(sender) {
211+
return (chatId, message) => {
212+
io.emit(
213+
`${MESSAGE_RECIEVED}-${chatId}`, //MESSAGE_RECIEVED event
214+
createMessage({ message, sender }),
215+
sendMessageToChatSAVE(chatId, message, sender)
216+
);
217+
};
218+
}
219+
220+
function sendMessageToChatSAVE(chatId, message, sender) {
221+
//console.log("communityChat.isCommunity is : " + communityChat.id);
222+
223+
//console.log("chatId inside sendMessageToChatSAVE is : " + chatId);
224+
if (chatId === communityChat.id) {
225+
//if chatId equal to community chat id
226+
console.log("we are saving communityChat messages!!");
227+
console.log("here we save chat in database!!");
228+
const newCommunitychats = new Communitychats({
229+
id: chatId,
230+
time: getTime(new Date(Date.now())),
231+
message: message,
232+
sender
233+
});
234+
235+
newCommunitychats
236+
.save()
237+
.then(newCommunitychats => {
238+
console.log("chat saved successfully!!");
239+
}) //here we send back new user register information as a response from the success server side
240+
.catch(err => {
241+
console.log("chat saved error is :" + err);
242+
});
243+
} else {
244+
console.log("we cannot save private message!!");
245+
246+
//console.log("chatId inside sendMessageToChatSAVE is : " + chatId);
247+
}
248+
}
249+
250+
/*
251+
* Adds user to list passed in.
252+
* @param userList {Object} Object with key value pairs of users
253+
* @param user {User} the user to added to the list.
254+
* @return userList {Object} Object with key value pairs of Users
255+
*/
256+
function addUser(userList, user) {
257+
let newList = Object.assign({}, userList);
258+
newList[user.name] = user;
259+
return newList;
260+
}
261+
262+
/*
263+
* Removes user from the list passed in.
264+
* @param userList {Object} Object with key value pairs of Users
265+
* @param username {string} name of user to be removed
266+
* @return userList {Object} Object with key value pairs of Users
267+
*/
268+
function removeUser(userList, username) {
269+
let newList = Object.assign({}, userList);
270+
delete newList[username];
271+
return newList;
272+
}
273+
274+
/*
275+
* Checks if the user is in list passed in.
276+
* @param userList {Object} Object with key value pairs of Users
277+
* @param username {String}
278+
* @return userList {Object} Object with key value pairs of Users
279+
*/
280+
function isUser(userList, username) {
281+
return username in userList;
282+
}
283+
284+
/*
285+
* @param date {Date}
286+
* @return a string represented in 24hr time i.e. '11:30', '19:30'
287+
*/
288+
const getTime = date => {
289+
return `${date.getHours()}:${("0" + date.getMinutes()).slice(-2)}`;
290+
};

client/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

client/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2+
3+
## Available Scripts
4+
5+
In the project directory, you can run:
6+
7+
### `npm start`
8+
9+
Runs the app in the development mode.<br>
10+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
12+
The page will reload if you make edits.<br>
13+
You will also see any lint errors in the console.
14+
15+
### `npm test`
16+
17+
Launches the test runner in the interactive watch mode.<br>
18+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19+
20+
### `npm run build`
21+
22+
Builds the app for production to the `build` folder.<br>
23+
It correctly bundles React in production mode and optimizes the build for the best performance.
24+
25+
The build is minified and the filenames include the hashes.<br>
26+
Your app is ready to be deployed!
27+
28+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29+
30+
### `npm run eject`
31+
32+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33+
34+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35+
36+
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37+
38+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39+
40+
## Learn More
41+
42+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43+
44+
To learn React, check out the [React documentation](https://reactjs.org/).
45+
46+
### Code Splitting
47+
48+
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49+
50+
### Analyzing the Bundle Size
51+
52+
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53+
54+
### Making a Progressive Web App
55+
56+
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57+
58+
### Advanced Configuration
59+
60+
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61+
62+
### Deployment
63+
64+
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65+
66+
### `npm run build` fails to minify
67+
68+
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

0 commit comments

Comments
 (0)