question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling subscribe and unsubscribe methods multiple times

See original GitHub issue

Describe the bug I have a download button component on a web page. This page will be the most visited page. You can think it’s a product page so I need to use my resources very efficient.

When a user clicks the download button, the server responds with a URL or calls the download service (trigger a background job) and responds with 200. A spinner icon appears on the download button, so the user understands he/she needs to wait.

User can click download button multiple times if he/she wants.

I want to initiate the action cable process on demand so I call the below code on download button click function.

this.$cable.subscribe({ channel: 'DownloadChannel', id: this.recordId });

After download service completes its job, it broadcasts to DownloadChannel with a URL.

On received function, I call the below code and the user starts downloading the file.

this.$cable.unsubscribe('DownloadChannel'); window.location = data.url;

After the second click on the download button, an error appears.

To Reproduce Please see download_button.vue component for testing purpose

Steps to reproduce the behavior:

  1. debug: true, debugLevel: ‘all’
  2. Click download button Successfully connected to channel 'DownloadChannel'. Unsubscribing from channel 'DownloadChannel'.
  3. Click download button again Uncaught TypeError: Cannot read property '_uid' of undefined

Expected behavior User can click download button multiple times if he/she wants. Calling unsubscribe method removes the DownloadChannel from this._channels object. https://github.com/mclintprojects/actioncable-vue/blob/b1d57527c65ff3484a143c3deb70442d27a7d874/src/cable.js#L103 _addChannel invoke only once when the component mounted. https://github.com/mclintprojects/actioncable-vue/blob/b1d57527c65ff3484a143c3deb70442d27a7d874/src/cable.js#L176 If the subscribe method can invoke the _addChannel method then my problem will resolve.

Screenshots After the second click on the download button, an error appears on this line. Uncaught TypeError: Cannot read property '_uid' of undefined https://github.com/mclintprojects/actioncable-vue/blob/b1d57527c65ff3484a143c3deb70442d27a7d874/src/cable.js#L201

Variables on this line after first click Screen Shot 2019-10-17 at 15 14 09

Variables on this line after second click Screen Shot 2019-10-17 at 15 08 39

Websocket network after second click Screen Shot 2019-10-17 at 17 09 08

Plugin version (please complete the following information): "actioncable-vue": "1.3.0",

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mclintprojectscommented, Oct 19, 2019

Hey, @rokumatsumoto! Thanks for getting back to me. I now understand what you want to do.

Your use-case made me realize that I never even added a way to re-subscribe if a user intentionally unsubscribed. It also made me realize that invoking unsubscribe should do only https://github.com/mclintprojects/actioncable-vue/blob/b1d57527c65ff3484a143c3deb70442d27a7d874/src/cable.js#L203 instead of removing the channel (should only happen when the component is destroyed). If I do that, I won’t have to make you pass in a this when subscribing.

I’ll add this fix in an update before the end of day.

1reaction
mclintprojectscommented, Oct 24, 2019

@rokumatsumoto I just published a patch (v1.4.2) that adds the log statement when you unsubscribe.

No, you don’t need to. If you unsubscribe from a channel, the resources you’ll still be using are negligible. Think of it as if you have an iron plugged into a socket that’s turned off. You’re not wasting any electricity. Also, it’s probably quicker to get back to using the iron because it’s already plugged into the socket.

If you want to disconnect a client though, it has to be from the server-side. Use the answers from this SO post.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Method containing a Subscribe is called multiple times, should ...
Yes - you should be unsubscribing if the subscription is no longer needed. An example using the take operator: this.chatService .
Read more >
Why is my subscribe listener being called multiple times per ...
The most common reason for this is that you have called subscribe() multiple times. Every time it is called it adds a new...
Read more >
Angular Observable Subscribe CallBack Getting Triggered ...
I had scenario where the subcribe callback was getting called multiple times. Before getting into the issue, let me give you a bit...
Read more >
6 Ways to Unsubscribe from Observables in Angular
1. Use the unsubscribe method ... A Subscription essentially just has an unsubscribe() function to release resources or cancel Observable ...
Read more >
Best Practices for Managing RxJS Subscriptions - This Dot Labs
This is great; however, when working with RxJS, you will likely have more than one subscription. Calling unsubscribe for each of them could ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found