interface Message {
user_id: number;
username: string;
content: string;
timestamp: Date;
animationValue?: Animated.Value;
}
useEffect(() => {
if (!token || !eventId) return; // Add eventId check
fetchEventDetails();
console.log("Fetching messages for event:", eventId);
fetchMessages();
}, [token, eventId]); const fetchMessages = async () => {
if (!user) return;
const messagesRef = collection(db, "events", "4", "chats", "default", "messages");
const messagesQuery = query(messagesRef, orderBy("timestamp", "desc"));
const unsubscribe = onSnapshot(messagesQuery, querySnapshot => {
// Add a log at the start of the callback to verify it runs.
console.log("onSnapshot callback triggered");
// Check if the snapshot is empty
if (querySnapshot.empty) {
console.log("No documents found in messages");
} else {
console.log("Snapshot data:", querySnapshot.docs.map(doc => doc.data())); // Log the data
}
setMessages(
querySnapshot.docs.map(doc => ({
user_id: doc.data().user_id,
username: doc.data().username,
content: doc.data().content,
timestamp: doc.data().timestamp ? doc.data().timestamp.toDate() : new Date(),
fileUrl: doc.data().fileUrl || "",
}))
);
});
return () => unsubscribe();
};
const renderMessage = useCallback(
({ item }: { item: Message }) => {
const isSender = item.user_id === userId;
const messageDate = item.timestamp;
return (
<Animated.View
style={[
styles.messageContainer,
isSender ? styles.sender : styles.receiver,
{ opacity: item.animationValue || 1 },
]}
>
<Text style={styles.messageText}>{item.content}</Text>
<Text style={styles.messageTime}>
{messageDate.toLocaleTimeString("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
})}
</Text>
</Animated.View>
);
},
[userId]
);
I am taking this one when open the chat page. @FrankvanPuffelen also, taking this on the log when I enter chat page. Auth working correctly but. Firestore is not working. Here is my firebaseConfig file:
import { initializeApp } from "firebase/app";
import { initializeAuth, getReactNativePersistence } from "firebase/auth";
import ReactNativeAsyncStorage from '@react-native-async-storage/async-storage';
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: **,
authDomain: **,
databaseURL: **,
projectId: **,
storageBucket: **,
messagingSenderId: **,
appId: **
};
// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const auth = initializeAuth(app, {
persistence: getReactNativePersistence(ReactNativeAsyncStorage)
}
);
export const db = getFirestore(app);
export const storage = getStorage(app);
Here is a logs:
>(NOBRIDGE) LOG onSnapshot callback triggered
>(NOBRIDGE) LOG No documents found in messages
>(NOBRIDGE) LOG onSnapshot callback triggered
>(NOBRIDGE) LOG No documents found in messages
>(NOBRIDGE)WARN [2025-02-11T17:00:55.993Z] @firebase/firestore: Firestore
> (11.3.0): WebChannelConnection RPC 'Listen' stream 0x512e1a58
> transport errored: {"defaultPrevented": false, "g": {"C": undefined,
> "F": null, "M": [Circular], "g": {"A": null, "Aa": 12, "B": 0, "C":
> null, "Ca": false, "D": "gsessionid", "Da": [Hc], "F": true, "G": 0,
> "H": [Object], "I": [T], "J": true, "K": "WFmJfthL_EMD2lhjW-y5bg",
> "L": 45000, "M": false, "O": true, "P": false, "R": 92, "S": [Object],
> "T": 0, "Ta": 5000, "U": 88681, "Ua": false, "Va": false, "W":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "Wa": 2, "X": true, "Xa": undefined, "Y": 1, "Ya": 1, "ba": true,
> "ca": undefined, "cb": 10000, "g": null, "h": [ic], "i": [Array],
> "ia": "", "j": [vb], "ja": undefined, "ka": null, "l": [Z], "la": 8,
> "m": null, "o": null, "pa": undefined, "qa": [T], "s": null, "u":
> null, "v": 0, "wa": 600000, "ya":
> "NlrUn6684y0jRyF-4aKII75hLaZwcrHBjwXwZhL3uy4", "za": -1}, "h":
> {"database": "projects/socius-0/databases/(default)"}, "i": {"g":
> [Object], "h": 4, "src": [Circular]}, "j": {"g": [Circular]}, "l":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "s": false, "u": true, "v": true}, "status": 1, "target": {"C":
> undefined, "F": null, "M": [Circular], "g": {"A": null, "Aa": 12, "B":
> 0, "C": null, "Ca": false, "D": "gsessionid", "Da": [Hc], "F": true,
> "G": 0, "H": [Object], "I": [T], "J": true, "K":
> "WFmJfthL_EMD2lhjW-y5bg", "L": 45000, "M": false, "O": true, "P":
> false, "R": 92, "S": [Object], "T": 0, "Ta": 5000, "U": 88681, "Ua":
> false, "Va": false, "W":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "Wa": 2, "X": true, "Xa": undefined, "Y": 1, "Ya": 1, "ba": true,
> "ca": undefined, "cb": 10000, "g": null, "h": [ic], "i": [Array],
> "ia": "", "j": [vb], "ja": undefined, "ka": null, "l": [Z], "la": 8,
> "m": null, "o": null, "pa": undefined, "qa": [T], "s": null, "u":
> null, "v": 0, "wa": 600000, "ya":
> "NlrUn6684y0jRyF-4aKII75hLaZwcrHBjwXwZhL3uy4", "za": -1}, "h":
> {"database": "projects/socius-0/databases/(default)"}, "i": {"g":
> [Object], "h": 4, "src": [Circular]}, "j": {"g": [Circular]}, "l":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "s": false, "u": true, "v": true}, "type": "c"}