I'm connecting to node servers via a TCP socket and am having issues with the data being written to the socket duplicating on each subsequent call.
My server call the following function that writes to each connected client:
clients.forEach(function (client) {
var string = "hello";
client.write(string);
client.on('data', function(data) {
console.log(data.toString());
});
});
This works and broadcasts the string "hello" to each of the connected sockets. The idea is that each client will reply with a response which will be output to console.log.
On each client I have the following:
client.on('data', function(data) {
console.log(data);
var response = "Hello back";
console.log('RES: ' + response);
client.write(response);
});
Each client receives hello and responds to the request. The issue arises that on each N response the server receives everything N times from each client.
Therefore on the first exchange it is simply
Hello
Hello Back
but on the second it is:
Hello
Hello Back
Hello Back
Any ideas why this could be happening? Do I need to flush the socket on each exchange or something?