Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Adapt my chat app to work with socket.io rooms

I have this chat app that I'm working on for a school project, and I need to improve it a little bit since it is pretty basic and simple. How can I make it so users can choose the room where they want their messages to be sent to? As a start, I created 4 room buttons that change the window.location.href (for example, if you click on the "room1" button, it changes the link to "http://127.0.0.1:5500/Client/index.html#room1", this way I can grab the room name and use it later, but I'm not sure if it is the right approach so I'll just change it if suggested). What would be the next steps?

Client side:

 //This changes the link so I can grab the room name when needed
 room1Btn.addEventListener('click', () =>{
        window.location.href='#room1';      
    })


 //This sends the message to the server when clicking on the Submit button
 sendBtn.addEventListener('click', () =>{
        sendMessage();
        })
 function sendMessage(){
        if(messageInput.value === ""){
            return;
        } else {
            const text = messageInput.value;
            socket.emit('message', text);
            socket.emit('sendName', name);
                       
        //This function displays the message
        messageDisplay();
        }
    }

Server side:

io.on('connection', (socket) => { 
 
        //Sends the message to the clients
        socket.on('message', (message) => {
            socket.broadcast.emit('message', `${message}`);        
        });
});

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda