Preventing Socket.io from firing multiple times for each event

I am trying to send an event from my server to the client when the client clicks on a button. The problem I'm running into is that my client code gets executed multiple times for every time my server fires an event. How can I ensure that my client code only fires once for each event emitted by the server?

My client code looks like this:

socket.on('data', function(data) {   
  $('.button').on('click, function() {
    clientFunc(data)
  });
});

      

My server code looks like this:

serverFunc(json, function(data) {
  socket.emit('data', data);
});

      

When reading the Node documentation, it seems like I might need to remove event listeners so that it clientFunc

only fires once per event?

+3


source to share





All Articles