Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!