Skip to content

Commit

Permalink
Added a realtime child listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Varshithvhegde committed Oct 18, 2023
1 parent 12d33ab commit 0ad4226
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/app/ContentService/contentresolver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export class ContentResolver implements Resolve<string | null> {

resolve(route: ActivatedRouteSnapshot) {
const routeId = route.paramMap.get('id');

if (routeId) {
const db = getDatabase();
const docRef = ref(db, routeId);
Expand Down
30 changes: 27 additions & 3 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Component, OnInit } from '@angular/core';
import { query } from '@angular/fire/database';
import { ActivatedRoute } from '@angular/router';
import { getDatabase, ref, set, get, DataSnapshot } from 'firebase/database';
import {
getDatabase,
ref,
set,
get,
DataSnapshot,
Database,
onChildChanged,
Query,
} from 'firebase/database';

@Component({
selector: 'app-home',
Expand All @@ -10,7 +20,7 @@ import { getDatabase, ref, set, get, DataSnapshot } from 'firebase/database';
export class HomeComponent implements OnInit {
routeId: string | null;
text: string | null = null;

textref: any;
constructor(private route: ActivatedRoute) {
this.routeId = this.route.snapshot.paramMap.get('id');
this.text = this.route.snapshot.data['content']; // Resolved content
Expand All @@ -33,6 +43,16 @@ export class HomeComponent implements OnInit {
.catch((error) => {
console.error('Error:', error);
});

// Set up a real-time listener for changes in the database
const db = getDatabase();
const docRef = ref(db, this.routeId);

// Listen for changes in a specific child ('text' in this case)
onChildChanged(docRef, (snapshot) => {
// console.log('Child changed:', snapshot.val());
this.text = snapshot.val() || null;
});
} else {
console.error('Route ID is null or undefined');
}
Expand All @@ -57,7 +77,9 @@ export class HomeComponent implements OnInit {
});
}

async checkIfRouteIdExistsInRealtimeDatabase(routeId: string): Promise<string | null> {
async checkIfRouteIdExistsInRealtimeDatabase(
routeId: string
): Promise<string | null> {
const db = getDatabase();
const docRef = ref(db, routeId);

Expand Down Expand Up @@ -96,4 +118,6 @@ export class HomeComponent implements OnInit {
console.error('Route ID is null or undefined');
}
}

// setup a realtime listener for text from realtime database
}

0 comments on commit 0ad4226

Please sign in to comment.