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??