// server.js
constpath=require("path");consthttp=require("http");constexpress=require("express");constsocketio=require("socket.io");constformatMessage=require("./utils/messages");const{userJoin,getCurrentUser,userLeave,getRoomUsers,}=require("./utils/users");constapp=express();constserver=http.createServer(app);constio=socketio(server);// Set static folder
app.use(express.static(path.join(__dirname,"public")));constbotName="ChatCord Bot";// Run when client connects
io.on("connection",(socket)=>{socket.on("joinRoom",({username,room})=>{constuser=userJoin(socket.id,username,room);socket.join(user.room);// Welcome current user
socket.emit("message",formatMessage(botName,"Welcome to ChatCord!"));// Broadcast when a user connects
socket.broadcast.to(user.room).emit("message",formatMessage(botName,`${user.username} has joined the chat`));// Send users and room info
io.to(user.room).emit("roomUsers",{room:user.room,users:getRoomUsers(user.room),});});// Listen for chatMessage
socket.on("chatMessage",(msg)=>{constuser=getCurrentUser(socket.id);io.to(user.room).emit("message",formatMessage(user.username,msg));});// Runs when client disconnects
socket.on("disconnect",()=>{constuser=userLeave(socket.id);if(user){io.to(user.room).emit("message",formatMessage(botName,`${user.username} has left the chat`));// Send users and room info
io.to(user.room).emit("roomUsers",{room:user.room,users:getRoomUsers(user.room),});}});});constPORT=process.env.PORT||3000;server.listen(PORT,()=>console.log(`Server running on port ${PORT}`));
// public/js/main.js
constchatForm=document.getElementById("chat-form");constchatMessages=document.querySelector(".chat-messages");constroomName=document.getElementById("room-name");constuserList=document.getElementById("users");// Get username and room from URL
const{username,room}=Qs.parse(location.search,{ignoreQueryPrefix:true,});constsocket=io();// Join chatroom
socket.emit("joinRoom",{username,room});// Get room and users
socket.on("roomUsers",({room,users})=>{outputRoomName(room);outputUsers(users);});// Message from server
socket.on("message",(message)=>{console.log(message);outputMessage(message);// Scroll down
chatMessages.scrollTop=chatMessages.scrollHeight;});// Message submit
chatForm.addEventListener("submit",(e)=>{e.preventDefault();// Get message text
constmsg=e.target.elements.msg.value;// Emit message to server
socket.emit("chatMessage",msg);// Clear input
e.target.elements.msg.value="";e.target.elements.msg.focus();});// Output message to DOM
functionoutputMessage(message){constdiv=document.createElement("div");div.classList.add("message");div.innerHTML=`<p class="meta">${message.username} <span>${message.time}</span></p>
<p class="text">
${message.text} </p>`;document.querySelector(".chat-messages").appendChild(div);}// Add room name to DOM
functionoutputRoomName(room){roomName.innerText=room;}// Add users to DOM
functionoutputUsers(users){userList.innerHTML=`
${users.map((user)=>`<li>${user.username}</li>`).join("")} `;}