Trigger to detect changes in firebase











up vote
0
down vote

favorite












When a child is updated or created in firebase database, I need to trigger a REST API. How to go about it?
Tried using "stream" function, but in order to invoke stream everysec will have to create a cron job. I would like some trigger to be done through firebase to call API.










share|improve this question
























  • I'm sorry but this question is a mess. What it has to do with Django ? What did you tried so far ? Could you please, share some code.
    – MrAleister
    Nov 19 at 10:30










  • My intention is to sync firebase data with psql, i'm using django api to do one time data dump currently. But i need to update my psql database when data is updated in firebase. To do so i need trigger to invoke my api when firebase data is updated
    – Manasa
    Nov 19 at 10:39










  • You say you "Tried using 'stream' function", which I assume means streaming data from the REST API. That API should not require a CRON job to constantly poll, but it will require a process that keeps its connection open. If you want us to help with that implementation, update your question to show what you tried.
    – Frank van Puffelen
    Nov 19 at 15:46










  • Sorry for the confusion, i used "pyrebase" package to stream changes. Anyway i went ahead with cloud functions to resolve the issue. Thanks
    – Manasa
    Nov 20 at 13:53















up vote
0
down vote

favorite












When a child is updated or created in firebase database, I need to trigger a REST API. How to go about it?
Tried using "stream" function, but in order to invoke stream everysec will have to create a cron job. I would like some trigger to be done through firebase to call API.










share|improve this question
























  • I'm sorry but this question is a mess. What it has to do with Django ? What did you tried so far ? Could you please, share some code.
    – MrAleister
    Nov 19 at 10:30










  • My intention is to sync firebase data with psql, i'm using django api to do one time data dump currently. But i need to update my psql database when data is updated in firebase. To do so i need trigger to invoke my api when firebase data is updated
    – Manasa
    Nov 19 at 10:39










  • You say you "Tried using 'stream' function", which I assume means streaming data from the REST API. That API should not require a CRON job to constantly poll, but it will require a process that keeps its connection open. If you want us to help with that implementation, update your question to show what you tried.
    – Frank van Puffelen
    Nov 19 at 15:46










  • Sorry for the confusion, i used "pyrebase" package to stream changes. Anyway i went ahead with cloud functions to resolve the issue. Thanks
    – Manasa
    Nov 20 at 13:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











When a child is updated or created in firebase database, I need to trigger a REST API. How to go about it?
Tried using "stream" function, but in order to invoke stream everysec will have to create a cron job. I would like some trigger to be done through firebase to call API.










share|improve this question















When a child is updated or created in firebase database, I need to trigger a REST API. How to go about it?
Tried using "stream" function, but in order to invoke stream everysec will have to create a cron job. I would like some trigger to be done through firebase to call API.







django firebase firebase-realtime-database psql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 15:46









Frank van Puffelen

222k25363389




222k25363389










asked Nov 19 at 10:24









Manasa

55




55












  • I'm sorry but this question is a mess. What it has to do with Django ? What did you tried so far ? Could you please, share some code.
    – MrAleister
    Nov 19 at 10:30










  • My intention is to sync firebase data with psql, i'm using django api to do one time data dump currently. But i need to update my psql database when data is updated in firebase. To do so i need trigger to invoke my api when firebase data is updated
    – Manasa
    Nov 19 at 10:39










  • You say you "Tried using 'stream' function", which I assume means streaming data from the REST API. That API should not require a CRON job to constantly poll, but it will require a process that keeps its connection open. If you want us to help with that implementation, update your question to show what you tried.
    – Frank van Puffelen
    Nov 19 at 15:46










  • Sorry for the confusion, i used "pyrebase" package to stream changes. Anyway i went ahead with cloud functions to resolve the issue. Thanks
    – Manasa
    Nov 20 at 13:53


















  • I'm sorry but this question is a mess. What it has to do with Django ? What did you tried so far ? Could you please, share some code.
    – MrAleister
    Nov 19 at 10:30










  • My intention is to sync firebase data with psql, i'm using django api to do one time data dump currently. But i need to update my psql database when data is updated in firebase. To do so i need trigger to invoke my api when firebase data is updated
    – Manasa
    Nov 19 at 10:39










  • You say you "Tried using 'stream' function", which I assume means streaming data from the REST API. That API should not require a CRON job to constantly poll, but it will require a process that keeps its connection open. If you want us to help with that implementation, update your question to show what you tried.
    – Frank van Puffelen
    Nov 19 at 15:46










  • Sorry for the confusion, i used "pyrebase" package to stream changes. Anyway i went ahead with cloud functions to resolve the issue. Thanks
    – Manasa
    Nov 20 at 13:53
















I'm sorry but this question is a mess. What it has to do with Django ? What did you tried so far ? Could you please, share some code.
– MrAleister
Nov 19 at 10:30




I'm sorry but this question is a mess. What it has to do with Django ? What did you tried so far ? Could you please, share some code.
– MrAleister
Nov 19 at 10:30












My intention is to sync firebase data with psql, i'm using django api to do one time data dump currently. But i need to update my psql database when data is updated in firebase. To do so i need trigger to invoke my api when firebase data is updated
– Manasa
Nov 19 at 10:39




My intention is to sync firebase data with psql, i'm using django api to do one time data dump currently. But i need to update my psql database when data is updated in firebase. To do so i need trigger to invoke my api when firebase data is updated
– Manasa
Nov 19 at 10:39












You say you "Tried using 'stream' function", which I assume means streaming data from the REST API. That API should not require a CRON job to constantly poll, but it will require a process that keeps its connection open. If you want us to help with that implementation, update your question to show what you tried.
– Frank van Puffelen
Nov 19 at 15:46




You say you "Tried using 'stream' function", which I assume means streaming data from the REST API. That API should not require a CRON job to constantly poll, but it will require a process that keeps its connection open. If you want us to help with that implementation, update your question to show what you tried.
– Frank van Puffelen
Nov 19 at 15:46












Sorry for the confusion, i used "pyrebase" package to stream changes. Anyway i went ahead with cloud functions to resolve the issue. Thanks
– Manasa
Nov 20 at 13:53




Sorry for the confusion, i used "pyrebase" package to stream changes. Anyway i went ahead with cloud functions to resolve the issue. Thanks
– Manasa
Nov 20 at 13:53












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










If you are using the Firebase Realtime database you can use Cloud Functions that trigger on a write to your database. Here is some docs that explain it a bit more. An example from the docs is below. Basically on a create to the database at /messages/{pushId}/original it will trigger this code that you could process your logic in or call your rest api. You can also do this with Firestore as well.



exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return snapshot.ref.parent.child('uppercase').set(uppercase);
});





share|improve this answer





















  • I think onWrite would be better since the requirement is for both create and update
    – MrAleister
    Nov 19 at 10:57








  • 1




    Correct onWrite would better suit this instance if its the same endpoint, but if they are different rest APIs for create and update. you'd want onUpdate and onCreate.
    – Jack Woodward
    Nov 19 at 10:59










  • Will try this out. Thanks
    – Manasa
    Nov 19 at 11:18


















up vote
1
down vote













https://firebase.google.com/docs/reference/js/firebase.database.Reference#on



const ref = firebase.database().ref("node/you/want/to/observe");
ref.on('value', function(dataSnapshot) { //here you do your API call });


BTW - you don't have to 'invoke stream every sec'. You create on listener that will trigger whenever condition is met. Juts remember to turn it off when you're done.






share|improve this answer























  • Please be aware that above answer assumes you run this code in the browser. Solution by Jack Woodward is 'server side' and probably more suiting your needs (especially if your API call will require credentials)
    – MrAleister
    Nov 19 at 11:11










  • sure,willl try. Thanks
    – Manasa
    Nov 19 at 11:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372557%2ftrigger-to-detect-changes-in-firebase%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










If you are using the Firebase Realtime database you can use Cloud Functions that trigger on a write to your database. Here is some docs that explain it a bit more. An example from the docs is below. Basically on a create to the database at /messages/{pushId}/original it will trigger this code that you could process your logic in or call your rest api. You can also do this with Firestore as well.



exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return snapshot.ref.parent.child('uppercase').set(uppercase);
});





share|improve this answer





















  • I think onWrite would be better since the requirement is for both create and update
    – MrAleister
    Nov 19 at 10:57








  • 1




    Correct onWrite would better suit this instance if its the same endpoint, but if they are different rest APIs for create and update. you'd want onUpdate and onCreate.
    – Jack Woodward
    Nov 19 at 10:59










  • Will try this out. Thanks
    – Manasa
    Nov 19 at 11:18















up vote
2
down vote



accepted










If you are using the Firebase Realtime database you can use Cloud Functions that trigger on a write to your database. Here is some docs that explain it a bit more. An example from the docs is below. Basically on a create to the database at /messages/{pushId}/original it will trigger this code that you could process your logic in or call your rest api. You can also do this with Firestore as well.



exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return snapshot.ref.parent.child('uppercase').set(uppercase);
});





share|improve this answer





















  • I think onWrite would be better since the requirement is for both create and update
    – MrAleister
    Nov 19 at 10:57








  • 1




    Correct onWrite would better suit this instance if its the same endpoint, but if they are different rest APIs for create and update. you'd want onUpdate and onCreate.
    – Jack Woodward
    Nov 19 at 10:59










  • Will try this out. Thanks
    – Manasa
    Nov 19 at 11:18













up vote
2
down vote



accepted







up vote
2
down vote



accepted






If you are using the Firebase Realtime database you can use Cloud Functions that trigger on a write to your database. Here is some docs that explain it a bit more. An example from the docs is below. Basically on a create to the database at /messages/{pushId}/original it will trigger this code that you could process your logic in or call your rest api. You can also do this with Firestore as well.



exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return snapshot.ref.parent.child('uppercase').set(uppercase);
});





share|improve this answer












If you are using the Firebase Realtime database you can use Cloud Functions that trigger on a write to your database. Here is some docs that explain it a bit more. An example from the docs is below. Basically on a create to the database at /messages/{pushId}/original it will trigger this code that you could process your logic in or call your rest api. You can also do this with Firestore as well.



exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const original = snapshot.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return snapshot.ref.parent.child('uppercase').set(uppercase);
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 10:55









Jack Woodward

51129




51129












  • I think onWrite would be better since the requirement is for both create and update
    – MrAleister
    Nov 19 at 10:57








  • 1




    Correct onWrite would better suit this instance if its the same endpoint, but if they are different rest APIs for create and update. you'd want onUpdate and onCreate.
    – Jack Woodward
    Nov 19 at 10:59










  • Will try this out. Thanks
    – Manasa
    Nov 19 at 11:18


















  • I think onWrite would be better since the requirement is for both create and update
    – MrAleister
    Nov 19 at 10:57








  • 1




    Correct onWrite would better suit this instance if its the same endpoint, but if they are different rest APIs for create and update. you'd want onUpdate and onCreate.
    – Jack Woodward
    Nov 19 at 10:59










  • Will try this out. Thanks
    – Manasa
    Nov 19 at 11:18
















I think onWrite would be better since the requirement is for both create and update
– MrAleister
Nov 19 at 10:57






I think onWrite would be better since the requirement is for both create and update
– MrAleister
Nov 19 at 10:57






1




1




Correct onWrite would better suit this instance if its the same endpoint, but if they are different rest APIs for create and update. you'd want onUpdate and onCreate.
– Jack Woodward
Nov 19 at 10:59




Correct onWrite would better suit this instance if its the same endpoint, but if they are different rest APIs for create and update. you'd want onUpdate and onCreate.
– Jack Woodward
Nov 19 at 10:59












Will try this out. Thanks
– Manasa
Nov 19 at 11:18




Will try this out. Thanks
– Manasa
Nov 19 at 11:18












up vote
1
down vote













https://firebase.google.com/docs/reference/js/firebase.database.Reference#on



const ref = firebase.database().ref("node/you/want/to/observe");
ref.on('value', function(dataSnapshot) { //here you do your API call });


BTW - you don't have to 'invoke stream every sec'. You create on listener that will trigger whenever condition is met. Juts remember to turn it off when you're done.






share|improve this answer























  • Please be aware that above answer assumes you run this code in the browser. Solution by Jack Woodward is 'server side' and probably more suiting your needs (especially if your API call will require credentials)
    – MrAleister
    Nov 19 at 11:11










  • sure,willl try. Thanks
    – Manasa
    Nov 19 at 11:18















up vote
1
down vote













https://firebase.google.com/docs/reference/js/firebase.database.Reference#on



const ref = firebase.database().ref("node/you/want/to/observe");
ref.on('value', function(dataSnapshot) { //here you do your API call });


BTW - you don't have to 'invoke stream every sec'. You create on listener that will trigger whenever condition is met. Juts remember to turn it off when you're done.






share|improve this answer























  • Please be aware that above answer assumes you run this code in the browser. Solution by Jack Woodward is 'server side' and probably more suiting your needs (especially if your API call will require credentials)
    – MrAleister
    Nov 19 at 11:11










  • sure,willl try. Thanks
    – Manasa
    Nov 19 at 11:18













up vote
1
down vote










up vote
1
down vote









https://firebase.google.com/docs/reference/js/firebase.database.Reference#on



const ref = firebase.database().ref("node/you/want/to/observe");
ref.on('value', function(dataSnapshot) { //here you do your API call });


BTW - you don't have to 'invoke stream every sec'. You create on listener that will trigger whenever condition is met. Juts remember to turn it off when you're done.






share|improve this answer














https://firebase.google.com/docs/reference/js/firebase.database.Reference#on



const ref = firebase.database().ref("node/you/want/to/observe");
ref.on('value', function(dataSnapshot) { //here you do your API call });


BTW - you don't have to 'invoke stream every sec'. You create on listener that will trigger whenever condition is met. Juts remember to turn it off when you're done.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 11:05

























answered Nov 19 at 10:56









MrAleister

395110




395110












  • Please be aware that above answer assumes you run this code in the browser. Solution by Jack Woodward is 'server side' and probably more suiting your needs (especially if your API call will require credentials)
    – MrAleister
    Nov 19 at 11:11










  • sure,willl try. Thanks
    – Manasa
    Nov 19 at 11:18


















  • Please be aware that above answer assumes you run this code in the browser. Solution by Jack Woodward is 'server side' and probably more suiting your needs (especially if your API call will require credentials)
    – MrAleister
    Nov 19 at 11:11










  • sure,willl try. Thanks
    – Manasa
    Nov 19 at 11:18
















Please be aware that above answer assumes you run this code in the browser. Solution by Jack Woodward is 'server side' and probably more suiting your needs (especially if your API call will require credentials)
– MrAleister
Nov 19 at 11:11




Please be aware that above answer assumes you run this code in the browser. Solution by Jack Woodward is 'server side' and probably more suiting your needs (especially if your API call will require credentials)
– MrAleister
Nov 19 at 11:11












sure,willl try. Thanks
– Manasa
Nov 19 at 11:18




sure,willl try. Thanks
– Manasa
Nov 19 at 11:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372557%2ftrigger-to-detect-changes-in-firebase%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin