-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (57 loc) · 1.72 KB
/
index.js
File metadata and controls
64 lines (57 loc) · 1.72 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
import express from "express";
import { Server } from "socket.io";
import { createServer } from "http";
import bodyParser from "body-parser";
import path from "path";
const __dirname = path.resolve();
const PORT = 3000;
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer);
app.set("view engine", "ejs");
app.set("views", "views");
//serving static files css and javascript
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//logging method and routes
app.use((req, res, next) => {
console.info(`${req.method} --> ${req.originalUrl}`);
next();
});
//HOME ROUTE /
app.get("/", (req, res) => {
res.render("index", { title: "Web chat🗨️" });
});
app.get("/tech", (req, res) => {
res.render("tech", { title: "tech-chat🗨️" });
});
app.get("/social", (req, res) => {
res.render("social", { title: "social-chat🗨️" });
});
app.get("/cricket", (req, res) => {
res.render("cricket", { title: "cricket-chat🗨️" });
});
//name space for different rooms
const tech = io.of("/tech");
//events and listeners for socket.io
tech.on("connect", (socket) => {
console.info("user has joined the chat");
socket.on("userConnected", (data) => {
socket.join(data.room);
socket.broadcast
.to(data.room)
.emit("newUserMessage", "A new user has joined!");
});
socket.on("getMessage", (data) => {
const room = data.room;
const value = data.value;
tech.to(room).emit("postMessage", value);
});
socket.on("disconnect", () => {
console.log("user has been disconnected");
});
});
httpServer.listen(PORT, () => {
console.info(`The server is up and running on ${PORT}`);
});