-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (43 loc) · 1.56 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require('dotenv').config()
const path = require('path')
const express = require('express')
const http = require('http')
const app = express()
const socketio = require('socket.io')
// library to prevent profanity
const Filter = require('bad-words')
const filter = new Filter()
const server = http.createServer(app)
const io = socketio(server)
const publicPath = path.join(__dirname, './public')
app.use(express.static(publicPath))
let count = 0
io.on('connection', (socket) => {
// socket.emit sends the message to the current connected user
socket.emit('connected', 'Welcome to our app !')
// socket.broadcast.emit sends the message to all connected sockets except current user
socket.broadcast.emit('connected','A new user joined the chat')
socket.on('countChanged', () => {
count ++
// io.emit send message to all connected users
io.emit('countUpdated', count)
})
// we can send a delivery report back to user or server by using callback as third parameter
socket.on('messageSend', (message, callback) => {
if(filter.isProfane(message)){
return callback('Please mind your language !')
}
io.emit('readMessage', message)
callback()
})
socket.on('geolocationSend', (location) => {
socket.broadcast.emit('readGeolocation', location)
})
socket.on('disconnect', () => {
io.emit('readMessage','A user left the chat !')
})
})
const PORT = process.env.PORT || 3000
server.listen(PORT, ()=> {
console.log(`Application is running on port ${PORT}`)
})