Now, let's alter our application to be more interactive and enable posting messages to Firebase:
- First, let's add a pushData method to the Firebase service:
export const pushData = (endpoint, data) => {
return firebase.database().ref(endpoint).push(data);
}
We are using push method here because we want to push additional data. There are other methods--such as set, which overrides data and update, which updates a specific entry in a database.
You can read more about the use of these methods at
https://firebase.google.com/docs/database/web/read-and-write.
https://firebase.google.com/docs/database/web/read-and-write.
- Now, let's add a proper API call in our API service:
export const postMessage = (message) => {
if (Boolean(message)) {
pushData('messages', {
incoming: false,
message
})
}
}
- We check here whether a message is empty and use the pushData method...