You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
http.listen(port,()=>{console.log(`listening on port ${port}`)})app.get('/',(req,res)=>{res.sendFile(__dirname+'/index.html')})
setup socket.io
constio=require('socket.io')(http)io.on('connection',(socket)=>{console.log("Server connected.....")// fetch the data from clientsocket.on('message',(msg)=>{//console.log(msg)// send message to all conected browser or clientsocket.broadcast.emit('message',msg)})})
Client JS
call the socket server
constsocket=io()
send messages
msgerForm.addEventListener("submit",event=>{event.preventDefault();constmsgText=msgerInput.value;if(!msgText)return;letmsg={user : name,message:msgText,}fetchmessage(msg)appendMessage(name,PERSON_IMG,"right",msgText);msgerInput.value="";socket.emit('message',msg)// sync with mongodbsincWithdb(msg)});