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

169
Vistas
How can I pass functions using server-side variables from client to server?

I have an express server with socketio on node, and a client on localhost. As I've been trying to pass functions between them using socket.emit, I've noticed that I can't pass anonymous functions and bind them to my server's context (so that variables/statements are executed entirely on the server side). What I want to happen is that bar will be logged on the server side.

Here's an example:

//CLIENT SIDE
socket.emit('foo', () => console.log('bar'));
//SERVER SIDE
socket.on('foo', f => f());

When I run this code, bar gets logged onto the client side, rather than the server side. I also tried the following:

socket.on('foo', f => f.bind(this)());
socket.on('foo', f => {
 global['g'] = f; g();
};

But nothing seems to work. I even tried wrapping it in a secondary anonymous function with the same methods, but no luck. Thoughts??

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