Skip to content

Instantly share code, notes, and snippets.

@foxted
Created February 7, 2022 03:45
Show Gist options
  • Save foxted/172d7a18b9188a2a3d21f975aac452f8 to your computer and use it in GitHub Desktop.
Save foxted/172d7a18b9188a2a3d21f975aac452f8 to your computer and use it in GitHub Desktop.
async subscribe() {
this.error = false;
if(!this.isLoggedIn || !this.user) {
return ;
}
const onCallCreateNotificationToken = httpsCallable(this.$functions, ON_CALL_CREATE_NOTIFICATION_TOKEN);
const { data } = await onCallCreateNotificationToken();
if(typeof data === 'object') {
this.error = true;
return;
}
const client = connect(
process.env.STREAM_API_KEY,
data,
process.env.STREAM_APP_ID
);
this.feed = client.feed('notifications', this.user.uid);
this.feed.get({ limit: 10 }).then((data) => {
Object.assign(this.notifications, data);
});
this.subscription = await this.feed.subscribe((data) => {
if (data.new && data.new.length > 0) {
this.feed.get({ limit: 10 }).then((data) => {
Object.assign(this.notifications, data);
});
}
}).then(() => {
console.debug('Notification feed subscription successful');
}).catch((error) => {
console.error('Notification feed subscription failed', error);
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment