-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
29 lines (22 loc) · 804 Bytes
/
server.js
File metadata and controls
29 lines (22 loc) · 804 Bytes
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
const express = require('express')
const app = express();
const http = require('http').createServer(app); //server creation : http is the server
const PORT = process.env.PORT || 3000
http.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
})
//using middleware to access static files
app.use(express.static(__dirname + '/public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html'); //now this sending this file to server
})
//socket
const io = require('socket.io')(http)
io.on('connection', (socket) => {
console.log('Connected to the server')
//server will get or listen the message here
socket.on('message', (msg) => {
// console.log(msg);
socket.broadcast.emit('message',msg) //sends msg to everyone except the client
})
})