ng2-cable icon indicating copy to clipboard operation
ng2-cable copied to clipboard

Consuming 2 channels on one connection

Open jseravalli opened this issue 7 years ago • 2 comments

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.

jseravalli avatar Jun 10 '17 23:06 jseravalli

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

But the broadcaster.on( ChannelName, ...

is same.

bhaveshvyas007 avatar Nov 29 '17 15:11 bhaveshvyas007

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.

jaybloke avatar Dec 04 '17 07:12 jaybloke