-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathapp.js
71 lines (54 loc) · 1.58 KB
/
app.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
67
68
69
70
71
//require the express module
const express = require("express");
const app = express();
const chatRouter = require("./route/chatroute");
//database connection
const Chat = require("./models/Chat");
const connect = require("./dbconnect");
//require the http module
const http = require("http").Server(app);
// require the socket.io module
const io = require("socket.io");
const port = 5000;
//bodyparser middleware
app.use(express.json());
//routes
app.use("/chats", chatRouter);
//set the express.static middleware
app.use(express.static(__dirname + "/public"));
//integrating socketio
socket = io(http);
//setup event listener
socket.on("connection", socket => {
socket.on("disconnect", function () {
console.log("user disconnected");
});
//Someone is typing
socket.on("typing", data => {
socket.broadcast.emit("notifyTyping", {
user: data.user,
message: data.message
});
});
//when soemone stops typing
socket.on("stopTyping", () => {
socket.broadcast.emit("notifyStopTyping");
});
socket.on("chat message", async function (msg) {
//save chat to the database
let chatMessage = new Chat({ message: msg, sender: "Anonymous" });
await chatMessage.save();
//broadcast message to everyone in port:5000 except yourself.
socket.broadcast.emit("received", { message: msg });
});
});
http.listen(port, async () => {
connect
.then(() => {
console.log("Successfully connected to MongoDB");
})
.catch(err => {
console.error("Error connecting to MongoDB", err);
});
console.log("Running on Port: " + port);
});