I am connecting Angular JS application having multiple states(Routes) with Node server(Socket) running on back-end. When I visit some other state and come back to the state where socket code is written, it again gets registered and when an event fire, multiple times the function gets called, which affects the performance.
var socket = io.connect('127.0.0.1:3000');
socket.on("connect",function(){
console.log("connected");
});
socket.on("test-channel:App\\Events\\NewMessage", function(message) {
if (vm.questions.length < 3) {
console.log("question fetching started");
vm.fetch_q();
}
});
I tried by putting the io.connect('localhost') in service also and put only the event in controller, but then also it is getting fired multiple times.