Monday, 17 April 2017

Unable to send data from one Client to another in socket.io Node.JS

I have two clients which can interchange some data over socket.io. I also have a server. What i need to do is i want to send data from client 1 to client 2 over a socket and i am unable to figure out that how i can achieve it.Please note that client 1 and client 2 are different html pages.

Server.JS

    io.on('connection', function (socket) {
     socket.on('broadcast', function (message) {
            console.log(message);
            socket.broadcast.emit('message', message);
            });
            console.log("connected");

   });

Client1.JS

var socket = io.connect();
socket.emit('broadcast',"Broadcasting Message");
socket.on('message', function (data) {

     alert(data)
});

Client2.JS

 var socket = io.connect();
 socket.on('message', function (data) {

     alert(data)
    //socket.emit('message',"Hello world");
});



via Danish

No comments:

Post a Comment