So, I have created a project which is my final year project FYP. So, for that project I have to integrate a chatting module into which the farmer and the buyer can talk to each other. So, first of all we can just create a... You can use firebase realtime data base for real-time chatting between them. And also the farmer can send a text message to the expert.
Forexample we have farmer chat screen and for that we show this :
const { expertId, expertName } = route.params as any;
Getting the expertId from params and we can get the farmer from the firebase current user.
and that is the case for the expert chat screen we are getting the farmerid which is the firebase id for both.
So guide me step by step how to create this whole module into app.
Like for now we have farmer/chatscreen
and expert/chatscreen
and for both screen we are getting the data from previous screen.
There are seprate chat screen for farmer and expert. Also I need to store the images and audio on the realtime database. Tell me what chages do I need to made on the firebase.
This is my firebase config file. What changes we need to do here ?
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { initializeAuth, getReactNativePersistence } from 'firebase/auth';
import AsyncStorage from '@react-native-async-storage/async-storage';
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyA3_p03SKoRhslo4O3IRNSRRVdlenOr7wE",
authDomain: "agroboost-e1cc5.firebaseapp.com",
projectId: "agroboost-e1cc5",
storageBucket: "agroboost-e1cc5.appspot.com",
messagingSenderId: "149622729159",
appId: "1:149622729159:android:8c38b2758e03a5d59111e2"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const my_auth = initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage)
});
// Initialize Firestore
const db = getFirestore(app);
export { db };
export default app;
Write each and everything. The farmer can send text to expert. So create the files accordingly.
The theme is green show use that. Also add proper styling to chat screen. The current user message is on right side with the time stamp not secs.The other user message will be displayed on left. Hope you understand. I have attached the expert consultation screen from which the farmer will chat with the expert. The message tab is for expert with which it can chat with the farmer.
So I want the code for expert chat screen and farmer chat screen. Also make sure to add proper styling. Some libraries if we need to install. Just make it simple for now that the message is sended from farmer to expert and expert to farmer and all messages are stored in realtime database.
We don't want to use firebase storage as it is not free just text message that will be stored in real time db.
We don't want the enhanced screens just text sending between the farmer and expert. Don't add media.
I'll help you implement a real-time chat module for your AgroBoost application using Firebase Realtime Database. Let's create the farmer and expert chat screens with proper styling and functionality.
firebaseConfig.ts
utils/messageUtils.ts
farmer/ChatScreen.tsx
expert/ChatScreen.tsx
You'll need to set up proper security rules for your Realtime Database. Go to the Firebase Console, select your project, then go to Realtime Database > Rules and update them:
{
"rules": {
"chats": {
"$chatId": {
".read": "auth !== null && ($chatId.contains(auth.uid))",
".write": "auth !== null && ($chatId.contains(auth.uid))",
"messages": {
"$messageId": {
".validate": "newData.hasChildren(['senderId', 'text', 'timestamp']) &&
newData.child('senderId').val() === auth.uid"
}
}
}
}
}
}npm install firebase @react-native-async-storage/async-storageMessage Utilities: The messageUtils.ts file contains functions for:
Chat Screens: Both the farmer and expert chat screens:
Data Structure: Messages are stored in the Realtime Database with this structure:
/chats/{chatId}/messages/{messageId}
- senderId: string
- text: string
- timestamp: timestampThis implementation provides a simple but effective real-time chat system between farmers and experts, with proper styling and functionality as requested.