angular-websocket icon indicating copy to clipboard operation
angular-websocket copied to clipboard

Remove listener?

Open axos88 opened this issue 10 years ago • 2 comments

Hello!

It would be great if the onMessage() would return an ID that could be later used to REMOVE that listener.

I am currently using one global websocket that I attach to on every controller. After a routechange I need to remove the registered listeners, but there seems to be no good way to do that.

axos88 avatar Oct 06 '15 06:10 axos88

My current hack is this (obviously ugly and suboptimal)

socket.onMessage(function(message) { ... })
var websocketCB = socket.onMessageCallbacks.last()

(...)

var index = service.socket.onMessageCallbacks.indexOf(websocketCB)
service.socket.onMessageCallbacks.splice(index, 1)

axos88 avatar Oct 06 '15 06:10 axos88

Hm, I'm having the same issue... Quite critical and wondering why this is not just possible to simply remove the listener... This is even more terrible, that we have to remove the message callback, the error callback, and all other events associated with it.

Maybe a slight improvement to the workaround above, would be to remove the function based on its name (assuming this is named function and not an anonymous function which has been passed to the callback). Identifying the function name can be done like that ...onMessageCallbacks[index].fn.name

0cv avatar Oct 30 '15 16:10 0cv