How to execute a function with specified param inside of socket.io connection function

socket = io client
socket io emit to specific socket id
how socket io works
socket.io client example
socket.io tutorial
what is socket io used for
socket io game
socket = io acknowledgement

I need some help with callbacks in my node.js app. Im working with socket.io to emit the data. I need to create a function with an emit and specific param that is being calculated outside of io.on('connection') brackets. I need to fire that inner function only when i calculate the param later in my app. How can i do that?

Example code:

io.on('connection', (socket) => {
    function name(param)
    {
        socket.emit('name', param);
    }
    //rest of the socket.io code
}

//function that calculates the param to be executed above^
function()
{
    var param = somevalue;
    //How can i execute function that is in io.on brackets and pass the param to it from here?
}
io.on('connection', (socket) => {
    // 10 is "param" in Hello
    Hello(10, function(result){
        console.log("Result from Hello function: " + result);

        // you have still access to socket

    });
}

function Hello(param, callback)
{
    // code etc
    var result = param + 1;

    callback(result);
}

socket.io / parameters on connection, of handshaken object) way to get query parameter inside connection handler: Your mistake is that you call io.connect on mouse event, but you should call in io.on('connection', function(socket) { //connection handler of main namespace Actually you have to use query parameter if you want to make your app more  1) query is like GET parameters, so replace "param:value" with "param=value" (if you want to pass multiple parameters, do it as you usually do with URL: param=value&some_other_param=test) 2) There is an easier and more reliable (because there is no risk to access an undefined property of handshaken object) way to get query parameter inside connection handler:

You can call your function from your .onevent like this:

io.on('connection', (socket) => {
    var calculatedParam = calParam(param); //Assuming param exists in the current context...
    socket.emit('name', calculatedParam);
    //rest of the socket.io code
}

//function that calculates the param to be executed above^
function calParam(p)
{
    var calcP = p; //Obviously calculate something here...
    return calcP;
}

Socket.IO - Quick Guide, Socket.IO - Quick Guide - Socket.IO is a JavaScript library for real-time web To do this, modify the io.on ('connection', function(socket)) call to include the You can call the join method on the socket to subscribe the socket to a given  1) query is like GET parameters, so replace "param:value" with "param=value" (if you want to pass multiple parameters, do it as you usually do with URL: param=value&some_other_param=test) 2) There is an easier and more reliable (because there is no risk to access an undefined property of handshaken object) way to get query parameter inside

You can assign the return value of io to a variable and use it to emit events when needed. Something like this:

var socket = io('http://localhost');
socket.on('connect', function(){});
socket.on('disconnect', function(){});

io.on('event', (socket) => {
    getParam();
}

function name(param) {
    socket.emit('name', param);
}

function getParam()
{
    var param = somevalue;
    name(param);
}

Emit function parameters (Client and Server) do not mirror Socket.io , Emit function parameters (Client and Server) do not mirror Socket.io When running tests I am running into an issue were the API for mock-socket's emit function is passed to the callback function listening to the specified event name​. var socket = io('http://localhost:8080'); socket.on('connect', function  Sockets work based on events. There are some reserved events, which can be accessed using the socket object on the server side. In the Hello World example, we used the connection and disconnection events to log when a user connected and left. Now we will be using the message event to pass message

io.socket.on(), Start listening for socket events from Sails with the specified eventName . Triggers the provided callback function when a matching event is received. you can listen for other socket events or call methods like io.socket.get() immediately. If a socket's connection to the server was interrupted—perhaps because the server  Note: for support questions, please use one of these channels: stackoverflow or slack You want to: report a bug request a feature Current behaviour invoke a socket.emit inside a socket.on function does not work.

The Essential Guide to Flash CS4 AIR Development, Socket class makes it possible to create a socket connection and read/ write raw When you are connected, the connecthandler () callback function is executed and You can use the bytesAvailable property to specify the length parameter. the received result inside the result TextField: var fullResult: String = socket. if what you want is passing something from the client to the server, you can't do it inside onConnect function. what you need here is defining a server side function and call it from the client. also your server implementation is wrong. let's say your server side function is called

Hackish C++ Pranks & Tricks, Then a new thread is created with the CreateThread function. If you call it in the main thread, the window will become inactive and stop responding to messages. The third parameter of the CreateThread function is a pointer to a function that siniaddr. siaddr — the INADDRiANY flag is specified to receive connections  Execute a user defined function using Transact-SQL. Note: Visit user defined function and Create Function (Transact SQL for more information about user defined functions. Before you begin Limitations and restrictions. In Transact-SQL, parameters can be supplied either by using value or by using @parameter_name=value.

Comments
  • Can't you return the value when calling the function back to your .on event?
  • I need that socket.emit() to be in a function so i can execute it later at specific time but code that determines when is outside of this scope.
  • mmm can you provide more context? Are you going to make some asynchronous call to get that param and the 'emit' the value?
  • No, i just need to be able to call this inner function later and pass a param which is a number so i can emit the changes on the site.
  • The thing is that i want to fire the inner function later after other events happens. The code that determines when to execute the inner function is outside of io.on brackets.
  • In that case, you can create the name function outside and call it from your other function whenever needed.
  • I don't have access to socket.emit outside of io.on connection scope as far as i know.
  • Agree with @PrerakSola 's answer. Also, and i do not have a way to test it right know, maybe you can send 'socket ' as a parameter to that function that will do the calculation and use it to emit your values... not sure though.
  • Im on the server-side, so everything i do i need to do inside of io.on('connection', (socket) => { } am i right?