Thursday, 16 March 2017

How can I send data from server to client using socket.io

I have a node.js file called app.js and a html file called index.html. when I running the server, the server giving me some incremental number.I want to send those number from server to the client but I am getting an error called (node:4620) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 connection listeners added. Use emitter.setMaxListeners() to increase limit. I do not know how to solve this problem, please help me someone.

/app.js

server.on('published', function(packet, client) {
  console.log('Published', packet.payload);
  //console.log('client', client);

  sendMessageToClient(packet) // Incremental value send to these function
});

function   sendMessageToClient(packet) {

    var count = packet.payload.toString('utf-8'); //store the incremental value to the count

    io.sockets.on('connection', function(socket){
                console.log(count);
        socket.emit('message', count);
    });

}

/index.html

 <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
    <script>
                var socket = io.connect('localhost:5000');
                socket.on('message', function(data){
                console.log(data.message);
                });
    </script>



via joy

No comments:

Post a Comment