Firebase Chat Messages
In addition to the REST API endpoints, Tribe provides real-time chat functionality through Firebase Firestore. This is primarily used in the mobile application to enable real-time messaging.
Collection Structure
The Firebase chatMessages collection stores chat messages with the following structure:
{
"AuthorImg": "https://firebasestorage.googleapis.com/v0/b/tribe-master-app.appspot.com/o/users%2FB7xbW9IZGDP9wY73TGyd8tRuhik2%2Fuploads%2F1720646253046000.jpg?alt=media&token=734069d1-6a08-4e32-9939-f999f26cbb3f",
"AuthorName": "Bruce van Zyl",
"broadcastRef": {
"__ref__": "broadcasts/i5Nz7Ci7N2neBg7Nr44b"
},
"contentID": 0,
"date": {
"__time__": "2024-07-18T22:27:35.486Z"
},
"deleted": false,
"message": "We are working to resolve this!",
"user": {
"__ref__": "user/B7xbW9IZGDP9wY73TGyd8tRuhik2"
}
}
Fields
| Field | Type | Description |
|---|---|---|
AuthorImg | string | URL to the author's profile image |
AuthorName | string | Display name of the message author |
broadcastRef | reference | Reference to the broadcast document in the broadcasts collection |
contentID | integer | ID of the associated content (if applicable) |
date | timestamp | When the message was sent |
deleted | boolean | Whether the message has been deleted |
message | string | The content of the chat message |
user | reference | Reference to the user document in the user collection |
replies | array | Optional array of reply messages (see structure below) |
Reply Structure
Replies are stored as an array within the parent message document:
"replies": [
{
"AuthorImg": "https://firebasestorage.googleapis.com/v0/b/tribe-master-app.appspot.com/o/users%2FDfQstdwZ8U2GojCV1gNS%2Fuploads%2F1719014918793446.jpg?alt=media&token=008984a2-1763-4fab-bed0-5d4eaa155045",
"AuthorName": "Teresa Gwaltney",
"comment": "Thanks",
"deleted": false,
"postDate": {
"__time__": "2024-07-19T04:12:58.068Z"
},
"userref": {
"__ref__": "user/DfQstdwZ8U2GojCV1gNS"
}
}
]
Reading Chat Messages
To read chat messages for a specific broadcast, query the chatMessages collection where the broadcastRef field matches the broadcast reference:
const chatMessagesRef = firebase.firestore().collection("chatMessages");
const query = chatMessagesRef.where(
"broadcastRef",
"==",
firebase.firestore().doc("broadcasts/i5Nz7Ci7N2neBg7Nr44b")
);
query.orderBy("date", "asc").onSnapshot(snapshot => {
const messages = [];
snapshot.forEach(doc => {
messages.push({
id: doc.id,
...doc.data()
});
});
// Use the messages array
});
Creating Chat Messages
To create a new chat message:
const chatMessagesRef = firebase.firestore().collection("chatMessages");
await chatMessagesRef.add({
AuthorImg: "https://example.com/profile.jpg",
AuthorName: "John Doe",
broadcastRef: firebase.firestore().doc("broadcasts/i5Nz7Ci7N2neBg7Nr44b"),
contentID: 0,
date: firebase.firestore.FieldValue.serverTimestamp(),
deleted: false,
message: "Hello, world!",
user: firebase.firestore().doc("user/USER_ID")
});
Adding Replies
To add a reply to an existing message:
const messageRef = firebase.firestore().collection("chatMessages").doc("MESSAGE_ID");
await messageRef.update({
replies: firebase.firestore.FieldValue.arrayUnion({
AuthorImg: "https://example.com/profile.jpg",
AuthorName: "Jane Smith",
comment: "Great point!",
deleted: false,
postDate: firebase.firestore.FieldValue.serverTimestamp(),
userref: firebase.firestore().doc("user/USER_ID")
})
});
Deleting Messages
To mark a message as deleted:
const messageRef = firebase.firestore().collection("chatMessages").doc("MESSAGE_ID");
await messageRef.update({
deleted: true
});
Security Rules
Ensure your Firestore security rules are properly configured to control access to chat messages:
match /chatMessages/{messageId} {
allow read: if true;
allow create: if request.auth != null;
allow update: if request.auth != null &&
(resource.data.user == request.auth.uid ||
get(/databases/$(database)/documents/user/$(request.auth.uid)).data.role == 'admin');
allow delete: if false; // Use soft delete instead
}
Notes
- The Firebase chat system is primarily used in the mobile application.
- Messages are never physically deleted; they are marked with
deleted: true. - For high-volume chats, consider implementing pagination or limiting the number of messages loaded.
- The
broadcastReffield is used to associate messages with a specific broadcast or live event. - The
contentIDfield can be used to associate messages with specific content items.