-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (31 loc) · 890 Bytes
/
index.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
/*
Steps :
1. Initialise NPM .
2. Index Css js files .
3. Install dependencies Express ,socket.io
4. Create an express server
5. Front end part
6. Set Up Socket.io in server side and client side
7. Client Send message logic in Client js
8. recieve message on server and broadcast to all clients
*/
const express = require("express");
const app = express();
const http = require("http").createServer(app);
app.use(express.static('public'))
const PORT = process.env.PORT || 3000;
app.get("/",(req,res)=>{
res.sendFile(__dirname+"/index.html");
})
http.listen(PORT,()=>{
console.log("Server running on the port 3000");
})
// Socket
const io = require("socket.io")(http)
io.on("connection",(socket =>{
console.log("Connected...")
console.log("User ID "+ socket.id);
socket.on("message",(msg)=>{
socket.broadcast.emit("message",msg)
})
}))