Skip to content

Commit

Permalink
add a formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Cris Fandino committed Feb 5, 2021
1 parent 809a09c commit 2129617
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
34 changes: 22 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,37 @@ const app = express()
const http = require('http').createServer(app)
const io = require('socket.io')(http)
const path = require('path')
const { sendServerMessage } = require('./utils')
const { formatMsgObj } = require('./utils')
const { createUser, getUsersInRoom, removeUser } = require('./users')

app.use(express.static(path.join(__dirname, 'client/public')))

const SERVER_NAME = '_BOT_'
io.on('connection', (socket) => {
socket.on('send-message', ({ room, ...rest }) => {
socket.on('send-message', ({ room, ...rest }, callback) => {
// send the message to everyone in the room
socket.broadcast.to(room).emit('server-message', rest)
const formattedUser = formatMsgObj(rest)
socket.broadcast.to(room).emit('server-message', formattedUser)
callback && callback(formattedUser)
})

function sendServerMessage(room, message) {
socket.broadcast.to(room).emit(
'server-message',
formatMsgObj({
username: SERVER_NAME,
message,
}),
)
}
socket.on('join-room', (newUser, callback) => {
// Add the user to memory
const joinedUser = createUser(socket.id, newUser)

const { room, username } = joinedUser
// Join the user
// Notify the client who joined the chat
socket.join(room)
sendServerMessage(room, `${username} joined the chat`)

// Notify the client
sendServerMessage(socket, room, `${username} joined the chat`)

socket.broadcast.to(room).emit('user-join', joinedUser) // Emit an event to pass the user who join the chat
socket.broadcast.to(room).emit('user-join', joinedUser) // Emit an event to pass the user object who join the chat
const roomUsers = getUsersInRoom(room)
callback(roomUsers, joinedUser)
})
Expand All @@ -34,14 +42,16 @@ io.on('connection', (socket) => {
const user = removeUser(socket.id)
if (!user) return
const { room, username } = user
sendServerMessage(socket, room, `${username} left the chat`)
sendServerMessage(room, `${username} left the chat`)
socket.broadcast.to(room).emit('user-disconnect', user)
}

socket.on('disconnect', handleLeaveAndDisconnect)
socket.on('user-leave', handleLeaveAndDisconnect)
})

app.get('*', (req, res) => {
res.send('<h1> 404 </h1>')
})
const PORT = process.env.PORT || 3000

http.listen(PORT, () => {
Expand Down
13 changes: 6 additions & 7 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { format } = require('date-fns')

function uuid(prefix = '', date = true) {
let counter = 0
return {
Expand All @@ -8,11 +10,8 @@ function uuid(prefix = '', date = true) {
}

const uid = uuid('SERVERMSG')

exports.sendServerMessage = function (socket, room, message) {
socket.broadcast.to(room).emit('server-message', {
username: '☺',
id: uid.create(),
text: message,
})
exports.formatMsgObj = function (obj) {
obj.time = format(Date.now(), 'h:mm a')
obj.id = uid.create()
return obj
}

0 comments on commit 2129617

Please sign in to comment.