1

I have a vuejs component that streams an MP4 stream over http but once the component is destroyed the http connection stays open and keeps downloading content until i refresh the browser.

When the component is mounted i set a <video> tags src equal to the MP4 url, which in turn automatically opens the http connection to the MP4 and it starts downloading and displaying video, which is expected and desired. BUT when the component is destroyed in the destroyed() hook, the connection stays open and keeps downloading video.

I have tried to use XMLHttpRequest() to create the connection but thats not working either. Is there a way to essentially kill the http connection upon destroying the vue component?

vue component:

<template>
    <video muted :src="mp4Src"></video>
</template>

export default {
  data() {
    return {
        mp4Src: null
    }
  },
  mounted() {
      this.mp4Src = 'http://my-stream.com/123.mp4';
  },
  destroyed() {
    // Setting this to this.mp4Src = ''; doesnt work
    console.log('How to destroying stream?');
  }
}
2
  • Any code examples? Commented Jan 17, 2019 at 18:04
  • 1
    @AndrewShmig Editied Commented Jan 17, 2019 at 18:39

3 Answers 3

1

So in my case i was actually able to resolve this in the beforeDestroy() hook after finding out that my component wasnt being destroyed. The reason it wasnt being destroyed was because the components were dynamically created in a v-if loop and the :key binding wasnt a completely unique key, so this caused the components to not be updated/destroyed correctly. Once i corrected this :key binding it started to work as expected.

Sign up to request clarification or add additional context in comments.

Comments

0

Add a ref property to the video element:

<video ref="video" muted :src="mp4Src"></video>

Then, on destroyed hook get the MediaStream and stop it:

this
  .$refs
  .video
  .srcObject
  .getTracks()
  .forEach(track => track.stop());
this.$refs.video.srcObject = null;

1 Comment

.srcObject is null in both destroyed() and beforeDestroy() hooks.
0

If you look at the documentation, you should be using beforeDestroy

At this stage the instance is still fully functional

Instead of Destroyed api:

Vue instance have been unbound

Edit:

Seems like vue has trouble closing that connection in the video element. But I think the problem might be with the video element self. I read around I found that people had issues in the past to properly unload a video element.

I have created a work around, seems to close connections. Codepen

beforeDestroy() {
   this.$refs["mp4"].load();
 }

4 Comments

@JeremyWalters, it's not the Vue problem, it's the browser problem. Have searched answers for similar questions - the only working solution is to completely remove HTML-element and than re-create it.
@AndrewShmig Thanks for confirming. Suspected that. Have you looked at my Codepen, that seems to work? You welcome to edit your finding in the answer.
@JeremyWalters, yes, I have seen your codepen and had another question: how do we measure and check if the browser is still loading the video or not in background? Your solutions seems fine.
Open devtools: Disabled Cache. Open Network Tab. Select Media Filter. When you play the video you would see it downloading. When you press the stop button it would stop downloading. Destroying it normally without ` this.$refs["mp4"].load();`, you will it still downloads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.