admin管理员组文章数量:1134573
Going through the docs, I encountered:
...you can call functions directly with an HTTP request or a call from the client.
~ source
there (link in the quote) is a mention about functions.https.onCall
.
But in the tutorial here, another function functions.https.onRequest
is used, so which one should I use and why? What is the difference/similarity between them?
Documentation for functions.https
is here.
Going through the docs, I encountered:
...you can call functions directly with an HTTP request or a call from the client.
~ source
there (link in the quote) is a mention about functions.https.onCall
.
But in the tutorial here, another function functions.https.onRequest
is used, so which one should I use and why? What is the difference/similarity between them?
Documentation for functions.https
is here.
- 1 Possible duplicate of Is the new Firebase Cloud Functions https.onCall trigger better? – Doug Stevenson Commented Jun 27, 2018 at 16:16
- Thank you @DougStevenson, but I have read that question prior to asking this one and it did not help me understand the topic better. – Qwerty Commented Jun 27, 2018 at 17:41
- I don't think there's anything else to understand. What is your specific confusion? – Doug Stevenson Commented Jun 27, 2018 at 17:47
- 2 @DougStevenson For one, there seems to be a difference in how those functions can be invoked. One via url, other using an in-app call. – Qwerty Commented Jun 27, 2018 at 18:22
- 2 @DougStevenson Can you check my answer for possible mistakes please? Thanks! – Qwerty Commented Jul 23, 2018 at 11:39
3 Answers
Reset to default 221The official documentation for these concepts is quite helpful, but from the view of an amateur, the described differences were confusing at first.
Both types, when deployed, are assigned with a unique HTTPS endpoint URL and can be accessed directly using an https client.
However, there is one important difference in the way how they are intended to be called.
onCall
: from the client'sfirebase.functions()
onRequest
: via standard https client (e.g.fetch()
API in JS)
onCall
Can be invoked directly from the client app, which is its primary purpose.
functions.httpsCallable('getUser')({uid}) .then(r => console.log(r.data.email))
It is implemented using user-provided
data
and automaticcontext
.export const getUser = functions.https.onCall((data, context) => { if (!context.auth) return {status: 'error', code: 401, message: 'Not signed in'} return new Promise((resolve, reject) => { // find a user by data.uid and return the result resolve(user); }) })
The
context
automatically contains metadata about the request such asuid
andtoken
.Input
data
andresponse
objects are automatically (de)serialized.
onRequest
Firebase onRequest Docs
Serves mostly as an Express API endpoint.
It is implemented with express
Request
andResponse
objects.export const getUser = functions.https.onRequest((req, res) => { // verify user from req.headers.authorization etc. res.status(401).send('Authentication required.'); // if authorized res.setHeader('Content-Type', 'application/json'); res.send(JSON.stringify(user)); })
Depends on user-provided authorization headers.
You are responsible for input and response data.
Read more here Is the new Firebase Cloud Functions https.onCall trigger better?
The main difference between onCall and onRequest for the client is the way they are invoked from client side. When you define a function using onCall e.g.
exports.addMessage = functions.https.onCall((data, context) => {
// ...
return ...
});
you can invoke it on the client side using the firebase function client SDK e.g.
// on the client side, you need to import functions client lib
// then you invoke it like this:
const addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
.then((result) => {
// Read result of the Cloud Function.
});
more info for onCall: https://firebase.google.com/docs/functions/callable
But if you define your function using onRequest e.g.
exports.addMesssage = functions.https.onRequest((req, res) {
//...
res.send(...);
}
you can call it using normal JS fetch API (no need to import firebase functions client lib on the client code) e.g.
fetch('<your cloud function endpoint>/addMessage').then(...)
this is the big difference that you need to consider when deciding on how to define your functions on the server.
more info for onRequest: https://firebase.google.com/docs/functions/http-events
One caveat I ran into was that the data
parameter in functions.https.onCall
is actually the raw request. To access the payload, we have to access the data
field of the input.
Example
Client sends payload to cloud function:
const callFunction = httpsCallable(functions, 'addMessage');
const result = await callFunction({payload: 'payload'});
Access payload in cloud function:
exports.addMessage = functions.https.onCall((rawRequest, context) => {
const payload = rawRequest.data.payload;
return ...
});
I couldn't find this in the docs, please let me know if I'm missing something.
本文标签: javascriptFirebase Cloud Functions Difference between onRequest and onCallStack Overflow
版权声明:本文标题:javascript - Firebase Cloud Functions: Difference between onRequest and onCall - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736851175a1955510.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论