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

161
Visualizações
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 à 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