Communication with NodeJS

Tags: ,



Hey StackOverFlow Community,

I know I’m asking a very “simple” question, but I’m racking my brains through hours of googling.

Can someone answer me how I can request from the client (Js) to my NodeJS server .e.g. if someone presses a button that the server does something (e.g. a function).

Exactly the opposite is the case when a Function Serverside is executed, so that I can tell the client that it can get something displayed in the browser, for example

I hope that someone can help me and send a good documentation! Or maybe another useful contribution!

I thank you in advance

Answer

With socket.io Client

<button type="button" onClick = "d()">Do</button>

function d(){
        socket.emit('do', function(data){
            socket.send("Do");
        });
}

Server

io.sockets.on('connection', function(socket){
socket.on('do', function(data){
        console.log('Do');
    });
}

This is just one of the million ways you can solve your problem. I find this the simplest.



Source: stackoverflow