-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (33 loc) · 1.01 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
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const PORT = process.env.PORT || 3000;
const Chats = require("./msgSchema");
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/messages");
http.listen(PORT, () => {
console.log(`Server is running at Port ${PORT}`);
});
app.use(express.static(__dirname + "/backend"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// Socket.io
const io = require("socket.io")(http);
io.on("connection", (socket) => {
console.log("connected..");
Chats.find().then((result) => {
console.log(result);
socket.emit("receive chat", result);
});
socket.on("message", (msg) => {
const newMessage = new Chats({ username: msg.user, message: msg.message });
newMessage.save().then(() => {
socket.broadcast.emit("message", msg);
});
});
socket.on("typing", (name) => {
// console.log(name);
socket.broadcast.emit("typing", name);
});
});