1

I can't solve the problem. Could you help with this link socket connection to the server? ?

  • Code on Server side
// const { Socket } = require('socket.io')

// Node server
const io=require('socket.io')(8000)

const user={}

// connection come  // user joined 
// socket.io instance which listen all the connection (new user joined )
io.on('Connection',socket=>{  
    // basically used for particular joint connection  
    // user joint event this function will run 
    socket.on('new-user-joined', name=>{
      console.log('New User Joined',name);
      user[socket.id]=name;     // update user  
      // dispaly to all connection in the group 
      socket.broadcast.emit('user-joined',name);
    });

    socket.on('send' , socket=>{
       socket.broadcast.emit('receive',{message:message, name: user[socket.id]})
    }); 
})

// code at client side

const socket=io('http://localhost:8000') 
const form=document.getElementById('send_container')
const messageInput=document.getElementById('messageinp') 
const messageContainer= document.querySelector(".container")


let name=prompt("Enter your name to join");
socket.emit('new-user-joined' ,name);

I am getting these error Image

  • Connection refused means that the server process is not running. Check that the server is running and bound to port 8000 without issues before running the client. – Matt Clark Nov 25 '22 at 04:50

0 Answers0