Alpit Anand
Alpit Anand

Reputation: 1258

Why my io.on is being fired continuously?

I have been trying to use socket.io in the project, however, my io.on is being fired continuously. I am not able to understand why it is in the loop. I have cross-checked the versions, as suggested by other answers, its all same. Any help would be great. Here is the code for it Server:-

io.on('connection', async function(socket){
       //These both lines are being called again and again
        console.log("Test"); 
        socket.emit('new_token');
    });

And this would be a consumer in angular:

 this.socket.on('new_token', () => {
     // console.log(data);
        this.notifier.notify('warning','Embeded token has been updated. Reloading in 3 seconds. !');
        setTimeout(()=>{
          window.location.reload();
        }, 3000);

    });

Upvotes: 0

Views: 94

Answers (1)

Ankit Bahuguna
Ankit Bahuguna

Reputation: 598

Every time a webpage loads or reloads, a new socket connection is created. You are emitting 'new_token' on every connection then your listener is reloading the page via window.location.reload(), which will again trigger connection event and so on. So it's kind of infinite loop as you are just going back and forth.

Upvotes: 1

Related Questions