-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (30 loc) · 1.15 KB
/
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
const express = require('express');
//socket
const socket = require('socket.io');
const app = express();
const server = app.listen(4000, () => {
console.log('listening to requests ')
})
app.use(express.static('public'))
//make socket listen to server
//setup socket in backend
const io = socket(server);
//it is the channel bet. frontend and backend->
//fire when frontend start the connection
io.on('connection', (socket) => {
console.log("made socket connection",socket.id)
socket.on('chat',(data)=>{
// console.log(data)
//io.sockets refers to all the clients in chat-> each client have a socket
//.emit('chat',data) ---> send data to all clients in the chat
io.sockets.emit('chat',data) //if A send a message to B this message will be send to all chatters in this chat
})
socket.on('typing',(data)=>{
// console.log('from server',data)
if(data.messageOwner)
//socket --> is the client who send message now
//socket.broadcast.emit ---> to display the feedback in chat of clients except the sender
socket.broadcast.emit('typing',data)
data.messageOwner=""
})
})