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

273
Visualizações
How to partition an array from the server side

The bottom line is, I need to get an array of people who have entered the server (the name is entered before entering the site). I implemented it very badly, on the client side I create an array and on the server side the same array and they are transferred between each other, but the problem is that the array created on the client side is filled only in requests to the server, that is, in the local scope, to me you need to access it globally. Here's the implementation: Client part:

const userName = prompt("Enter your name:");
nameBlock.innerHTML = userName;

let arrayUser = []; // array of users

socket.emit('connect user', {
    userConnect: userName, // pass the name
    arrayConnect: arrayUser // we pass the array where we write the names
});

socket.on('connect user', (userName) => {
    arrayUser.push(userName.arrayConnect);
    const item = document.createElement('div');
    item.innerHTML = `User <b><span class = "user">${userName.name}</span></b> connected to the chat!`
    userConnect.append(item);
});

Server part:

let arrayUser = []; // create an array
io.on('connection', (socket) => {
    socket.on('chat message', (msg) => {
        io.emit('chat message', {
            message: msg.message,
            name: msg.name
        });
    });

    socket.on('connect user', (userName) => {
        arrayUser.push(userName.userConnect);
        console.log(arrayUser);
        io.emit('connect user', {
            name: userName.userConnect,
            arrayConnect: arrayUser
        });
    });
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