Deathslice246

Deathslice246

At the moment, I’m using angularjs with phoenix and I want to know how can I unbind custom channel events.

In Angularjs, one way would be like this.

var unbindHandler = $scope.$on("channel_event", channelFunction); // returns a deregistration function

.... later down the line
unbindHandler() // event is unbinded
unbindHandler = null

When I try to do the same thing with with the channel object.

    var unbindHandler = channel.on("new_message", message => {
       ... code
   })

and call it

    unbindHandler()

I get an error saying that unbindHandler is not a function because it is undefined. Because of this I have to assume that channel.on doesn’t return a deregistration function for me to use so I can unbind when I’m done. What can I do?

I’ve tried to leave the channel(channel.leave()) hoping that it would automatically unbind but it still doesn’t.

Showing Posts 1 to 5

voughtdq

voughtdq

It returns a ref:

var ref = channel.on("new_message", message => { ... });

channel.off("new_message", ref);

You could create a wrapper function:

function channelFunctionFactory(channel) {
   return function(event, callback) {
     var ref = channel.on(event, callback);
     return function() {
       channel.off(event, ref);
     }
   }
}

var channelFunction = channelFunctionFactory(channel);
var unbindHandler = channelFunction("new_message", message => { ... });
unbindHandler();

OK, so this is untested. I don’t know if it will work, but it gives you what you want (I think).

Deathslice246

Deathslice246 OP

Thanks for the reply, but at least with angularjs, I don’t think it returns a ref.

To verify I did this

var channel = socket.channel("lobby", {});
console.log(channel.on("new_message", () => {
     console.log("Hello");
 }));  // This prints out undefined
console.log(channel) // Yet it still says that it binded "new_message". 

So is there any other way to get a handler or ref to the event?

voughtdq

voughtdq

Ah! I know what’s wrong.

It looks like the ref logic is only available in master right now.

You can do this:

function channelFunctionFactory(channel) {
   return function(event, callback) {
     channel.on(event, callback);
     return function() {
       channel.off(event);
     }
   }
}

var channelFunction = channelFunctionFactory(channel);
var unbindHandler = channelFunction("new_message", message => { ... });
unbindHandler();

Unfortunately it means you can’t bind to the same event multiple times. You could also try getting the updated phoenix.js from master.

Deathslice246

Deathslice246 OP

Thanks, that worked. I figured I was only able to bind to only one event at a time considering that when I joined a different channel, the number of bindings for each channel object was off. Much appreciated.

Deathslice246

Deathslice246 OP

Though I have a question before I put this topic to rest.

If I do this

var channel = socket.channel("lobby", {});

Down the line I bind some events to that channel object and then I do this without unbinding the events

var channel = socket.channel("room", {});

Would it cause a memory leak for not calling channel.off on the previous channel object and initializing it with a new one? My intuition says yes but I’m not sure.

If not, then is there any point in unbinding in my case?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews