简体   繁体   English

Firebase 云函数:onRequest 和 onCall 之间的区别

[英]Firebase Cloud Functions: Difference between onRequest and onCall

Going through the docs, I encountered:通过文档,我遇到了:

...you can call functions directly with an HTTP request or a call from the client . ...您可以使用 HTTP 请求或来自客户端调用直接调用函数。

~ source ~ 来源

there (link in the quote) is a mention about functions.https.onCall .那里(引用中的链接)提到了functions.https.onCall

But in the tutorial here , another function functions.https.onRequest is used, so which one should I use and why?但是在这里的教程中,使用了另一个函数functions.https.onRequest ,那么我应该使用哪个,为什么? What is the difference/similarity between them?它们之间有什么区别/相似之处?

Documentation for functions.https is here . functions.https文档functions.https这里

The official documentation for those is really 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.这两种类型在部署时都分配有唯一的 HTTPS 端点 URL,可以直接访问。

onCall随传随到

  • Can be invoked (and this is also the main purpose) directly from the client app.可以直接从客户端应用程序调用(这也是主要目的)。

     functions.httpsCallable('getUser')({uid}) .then(r => console.log(r.data.email))
  • It is implemented with user-provided data and automagic context .它是通过用户提供的data自动context

     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 automagically contains metadata about the request such as uid and token . context自动包含有关请求的元数据,例如uidtoken
  • Input data and response objects are automatically (de)serialized.输入dataresponse对象会自动(反)序列化。

onRequest根据要求

  • Firebase onRequest Docs Firebase onRequest 文档
  • Serves mostly as an Express API endpoint.主要用作 Express API 端点。
  • It is implemented with express Request and Response objects.它是通过明确的RequestResponse对象实现的。

     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?在此处阅读更多信息新的 Firebase Cloud Functions https.onCall 触发器更好吗?

The main difference between onCall and onRequest for the client is the they way they are invoked.客户端的onCallonRequest之间的主要区别在于它们的调用方式。 when you define a function using onCall eg当您使用onCall定义函数时,例如

exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

you invoke on it the client side using firebase function client SDK eg您使用 firebase 功能客户端 SDK 在客户端调用它,例如

// 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有关 onCall 的更多信息: https ://firebase.google.com/docs/functions/callable

But if you define you function using onRequest eg但是,如果您使用onRequest例如定义您的功能

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 side) eg您可以使用普通的 JS fetch API 调用它(无需在客户端导入 firebase 函数客户端库)例如

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有关 onRequest 的更多信息: https ://firebase.google.com/docs/functions/http-events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM