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

142
Visualizações
Express call server event from client

I have an express app, with a server connected to a client. I have server data that I want to send to the client, and a button on the client that I want to trigger a function on the server. I have successfully implemented the server-> client connection through the following code:

client js

const source = new EventSource('/events');

    source.addEventListener('message', message => {
...
    });

server js

express()
...
    .get('/events', async function(req, res) {
        res.set({
          'Cache-Control': 'no-cache',
          'Content-Type': 'text/event-stream',
          'Connection': 'keep-alive'
        });
        res.flushHeaders();
        while (true) {
          await new Promise(resolve => setTimeout(resolve, 1000));
           res.write(`data: some text\n\n`);
          
        }
      })
...

This lets me send data to the client from the server. However, I also have a button on the client that I would like to call a function on the server. I think I've seen methods with ajax, but I am trying to avoid that if possible. Is there a way to do basically the reverse of the code above, where I can trigger a server event from the client (like a client sent event instead of the server sent event I have above), without ajax? Thanks

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