Skip to main content

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

FieldTypeDescription
AuthorImgstringURL to the author's profile image
AuthorNamestringDisplay name of the message author
broadcastRefreferenceReference to the broadcast document in the broadcasts collection
contentIDintegerID of the associated content (if applicable)
datetimestampWhen the message was sent
deletedbooleanWhether the message has been deleted
messagestringThe content of the chat message
userreferenceReference to the user document in the user collection
repliesarrayOptional 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 broadcastRef field is used to associate messages with a specific broadcast or live event.
  • The contentID field can be used to associate messages with specific content items.