You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Run firebase login and login with your firebase account
Select your project from the list shown to you
Run firebase init functions which creates a functions directory
Add your functions logic
Run firebase deploy
All Done!
Main Code Snippets of index.js:
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
var db = admin.database()
exports.sendCorrectPredictionNotifications = functions.database.ref('/referee-confirmed-events/{pushId}').onCreate((event) => {
let data = event.data.val()
let key = event.params.pushId
// return a specific value or do some asyn stuff by returning a promise
return new Promise((resolve, reject) => {
// do some async stuff here
})
})
^ This is the eventChannel function from Redux-Sagas which allows you to create a channel for your event listener which can then dispatch redux actions every time a specific event occurs.
function * startGameWatcher (gameID) {
const gamesChannelsRef = firebase.database().ref(`games/${gameID}`)
const game = channel(gamesChannelsRef, 'value')
try {
while (true) {
const data = yield take(game)
if (data.value) {
yield put(GamesActions.updateGameRequest(data.value))
if (data.value && data.value.status === 'ended') {
game.close()
}
}
}
} catch (error) {
game.close()
} finally {
if (yield cancelled()) {
game.close()
}
}
}
^ This is the watcher generator function which starts the event listener which you created above and creates a continuous loop in a generator function which loops/steps every time there is an event from the listener you created above. It then takes that data and you are then able to dispatch new actions of your choice.