Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda