Prevent server from crashing from inactive subscriptions


For testing purposes, I’v created a websocket that will emit.message every 10 seconds via setInterval.

    return setInterval(()=>{
        this.socket.emit('message','some message')

The problem is that when the user leaves the page, it’s still trying to send packets to the closed subscription/connection and I get the error:

 throw new Error(`Topic ${topic} doesn't have any active subscriptions`)

Does anyone have any ideas on how to prevent this?


Shouldn’t you be clearing the interval when the socket closes?

I’m clearing it, but it’s still happening.

Can you show bigger picture including how you clear interval?