viktor-shmigol / ng2-cable

Connect your Angular(2/4)/ionic(2/3) application with Rails ActionCable
https://ng2-cable-example.herokuapp.com
44 stars 14 forks source link

Consuming 2 channels on one connection #11

Open jseravalli opened 7 years ago

jseravalli commented 7 years ago

I have a question about how the code works, say I have 2 channels, NotificationsChannel and ChatsChannel, then I would go and do:

this.cable.subscribe(url, 'NotificationsChannel');
this.cable.subscribe(url, ChatsChannel');

since every subscribe call does a setCable which in turn calls ActionCable.createConsumer isn't this creating 2 connections to the same cable for the 2 channels?

Sorry if this obvious, but I wanted to make sure since I was thinking it might be possible to have the cable support several channels with the same consumer and have disconnect and unsubscribe(channel) methods.

bhaveshvyas007 commented 6 years ago

I have a same question, I want to subscribe to same channel with different parameters:

But the broadcaster.on( ChannelName, ...

is same.

jaybloke commented 6 years ago

I too am having the same problem... I'm finding it hard to grasp the set up. For example:

this.ng2Cable.subscribe(url, 'MessagesChannel');

Why do we need to subscribe to an initial channel when making the connection? Seems kind of redundant as I then also need to do the following:

this.broadcaster.on('MessagesChannel').subscribe(...)

Then If I want to subscribe to an additional channel, I need to do the following:

this.ng2Cable.subscribe(url, 'OtherChannel');
this.broadcaster.on('OtherChannel').subscribe(...)

Which unnecessarily opens up another WS connection to the server.

Perhaps this would be more intuitive:

this.subscription = this.ng2cable.connect(url: string).subscribe(); // connect to server
this.subscription.unsubscribe(); // disconnect from server
this.subscription = this.broadcaster.on('ChannelName').subscribe(...) // subscribe to channel
this.subscription.unsubscribe(); // unsubscribe from channel

Just my two cents worth.