Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do event listeners have to be removed manually when closing WebSocket connection in browser?

I'm using native methods provided by WebSocket API.

I'm wondering if I need to remove event listeners because removeEventListener method is not mentioned in the link nor in the MDN example code for websockets.

I'm using event methods like this:

const ws = new WebSocket(url);
ws.onopen = () => {}

In addition I'm wondering if I need to remove event listeners if I'm closing the connection with ws.close() method I suppose it will remove the listeners anyway as it's cleaning up.

like image 807
Yos Avatar asked Nov 23 '19 12:11

Yos


1 Answers

You would have to remove the event listeners manually. The fact is that added listeners will remain active on the element until they are destroyed or they are removed manually in modern browsers. You can try testing if the events are fired after the web socket connection has been closed.

like image 131
Abhishek Ranjan Avatar answered Oct 16 '22 20:10

Abhishek Ranjan