Skip to content

Instantly share code, notes, and snippets.

@sasin91
Created October 25, 2022 08:24
Show Gist options
  • Save sasin91/b7c0326723efe82df7565e5a1115540a to your computer and use it in GitHub Desktop.
Save sasin91/b7c0326723efe82df7565e5a1115540a to your computer and use it in GitHub Desktop.
ZomeTV Broadcast live stats
<script lang="ts">
export default {
props: {
jwtToken: String,
},
data() {
return {
intervalId: undefined,
liveViewers: 0,
stopsCount: 0,
};
},
render() {
return this.$scopedSlots.default({
liveViewers: this.liveViewers,
stopsCount: this.stopsCount,
fetch: this.fetch,
});
},
mounted() {
this.intervalId = setInterval(() => {
if (!this.jwtToken) {
this.$emit("refetch");
return;
}
this.fetch();
}, 1000);
},
beforeDestroy() {
clearInterval(this.intervalId);
},
methods: {
async fetch() {
const response = await fetch(`https://stats.mux.com/counts?token=${this.jwtToken}`);
const data: {
data: Array<{ views: number; viewers: number; updated_at: Date }>;
} = await response.json();
const viewers = data.data[0].viewers;
const stops = Math.abs(this.liveViewers - viewers);
this.liveViewers = viewers;
this.stopsCount = stops;
},
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment