Note that there are some explanatory texts on larger screens.

plurals
  1. POrunning custom functions with node and socket.io
    primarykey
    data
    text
    <p>So I would like to be able to run custom function through socket.io using node My basic idea is fairly simple, I just can't get it to work. Generally speaking you would write something similar to this to run a function on incoming message...</p> <pre><code>//server socket.on('incomingMessage', function(data) { console.log(data); io.sockets.emit('newMessage', {id: data.id, from: data.name, message: data.message}); }); //client socket.on('newMessage', function(data) { $('#messages').html($('#messages').html() + data.message); $('#from').html($('#from').html() + data.from); }); </code></pre> <p>but what I would like to do is allow that to be much more dynamic and actually pass the function I want to run in the message itself, so something like this...</p> <pre><code>//server socket.on('customFunction', function(data) { console.log(data); io.sockets.emit('runFunction', data); }) //client socket.on('runFunction', function(data) { for(var index in data) { args.push(data[index]); } data.fn.apply(this, args); }); </code></pre> <p>but when I call this from a client using something like...</p> <pre><code>socket.emit('customFunction', {id: sessionId, name: user.name, callBack: function() { alert('it worked'); } }); </code></pre> <p>on the server the only thing it logs is the session id and the name, the callBack(regardless of the name, I have tried func, run, cb, callBack, customFunc) just disappears. so I am kind of at a loss. Anyone have any ideas? thank you in advance for the help. I am not sure why, but when calling this it only logs the session id and the name elements, the callback, or fn, or whatever else, just isn't there. Does node or socket.io remove functions from arguments when sending things off?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload