Modify a variable from the server with Socket.io

1

I need to change some variables of a script from the view of my project, but I need to do it from the server or have the variable take the value I send it through websockets .

I tried the following:

//EN EL SERVER

io.sockets.on("connection",function(socket){

    socket.emit("NombreDelSocket",UnDatoJSON);
});


//EN EL CLIENTE

var Nombre;

var socket = io.connect(RUTA DEL SERVIDOR);

socket.on("NombreDelSocket",function(data){

   //data es UnDatoJSON que mando el servidor
    Nombre = data[0].nombre; 
    console.log(Nombre);
});

When I check the browser console, the console.log(Nombre) instruction does indeed print the name I wanted to send. Very good, but it does not change the name of the variable. If I show that message from the console outside of that method of socket , it will tell me that the name is undefined .

I do not know if you understand what I want to do. I just want to set a variable of a client script, but I do not know how to do it from websockets .

    
asked by bousing 27.10.2016 в 03:53
source

5 answers

2

The function that is passed to the socket.on("NombreDelSocket" method is executed at some point in time because it is an asynchronous event.

  

If I post that console message I show it outside of that socket method it will tell me that the name is undefined.

Since you have not initialized the variable Nombre the value is undefined . The error is that you try to log the value of the variable Nombre when the event NombreDelSocket is still not executed.

  

When I check the browser console, the console.log (Name) instruction prints the name I wanted to send, very well, but I do not change the name of the variable.

The value of the variable Nombre if it is modified, only not immediately, you must wait for the NombreDelSocket event to be executed.

One possible solution would be to invoke a function to notify that Nombre was modified.

For example:

//EN EL CLIENTE
var Nombre;
var socket = io.connect(RUTA DEL SERVIDOR);
socket.on("NombreDelSocket",function(data){

   //data es UnDatoJSON que mando el servidor
    Nombre = data[0].nombre; 
    avisarQueNombreFueSeteado();
});
function avisarQueNombreFueSeteado() {
   console.log(Nombre);
}
    
answered by 27.10.2016 в 04:31
0

One possible solution to your problem is to directly modify the html element with the value of the name received from the server.

For example, if what you have is an < p id="myP" > and you want the name of the person, what you can do is replace console.log (Name) with document.myP.innerHTML = Name, or using jQuery $ ('# myP'). text (Name). You must bear in mind that when that event is triggered, it is that you can show the data you want to the user.

    
answered by 29.10.2016 в 19:12
0

What you can do is make a view refresh, but that depends on how you are working on the modification of the views, example if it is jquery would do an update of the tag where the variable is or if it is angular, it would be to update a Scope variable, so that when the io socket signal arrives, the function suggested in the immediately previous response will be executed and the change in the sun or the scope refresh will be invoked

    
answered by 30.10.2016 в 01:53
0

I see that what you need to do is modify "something" using the value that comes from the server. You must bear in mind that the event NombreDelSocket is executed when the connection is established and that happens very surely after all your javascript was loaded and executed, so the variable Nombre is empty initially ..

in addition to the response from @marcos, I want to add: the solution then is simple, you must apply the changes when the event is called, something like this:

//EN EL CLIENTE

var Nombre;

var socket = io.connect(RUTA DEL SERVIDOR);

socket.on("NombreDelSocket",function(data){

   //data es UnDatoJSON que mando el servidor
    Nombre = data[0].nombre; 
    console.log(Nombre);
   // en este instante tu variable que tiene "algo" asi que puedes usarla.
    modificaAlgo();
    });

// funcion que utiliza la variable nombre, para hacer algo!
   function modificaAlgo(){
     // aqui escribes las instrucciones y puedes usar la variable Nombre que ya tiene algún valor.
   }
    
answered by 31.10.2016 в 21:15
0

You should use the callback:

var Nombre;

var socket = io.connect(RUTA DEL SERVIDOR);

socket.on("NombreDelSocket",function(data){

    actualizarVariable(data);
});

function actualizarVariable(data){
     //data es UnDatoJSON que mando el servidor
    Nombre = data[0].nombre; 
}
    
answered by 22.02.2017 в 18:49