forked from bitfumes/chat-app-with-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (31 loc) · 1009 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
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
})
io.on('connection', (socket) => {
io.emit('noOfConnections', Object.keys(io.sockets.connected).length)
socket.on('disconnect', () => {
console.log('disconnected')
io.emit('noOfConnections', Object.keys(io.sockets.connected).length)
})
socket.on('chat-message', (msg) => {
socket.broadcast.emit('chat-message', msg)
})
socket.on('joined', (name) => {
socket.broadcast.emit('joined', name)
})
socket.on('leaved', (name) => {
socket.broadcast.emit('leaved', name)
})
socket.on('typing', (data) => {
socket.broadcast.emit('typing', data)
})
socket.on('stoptyping', () => {
socket.broadcast.emit('stoptyping')
})
})
http.listen(3000, () => {
console.log('Server is started at http://localhost:3000')
})