使用firebase-queue返回任务结果 [英] Return task results using firebase-queue
问题描述
$ b
- 将用户手机号码发送到Nexmo API,并返回一个request_id成功,并通过短信向用户的手机发送代码
- 将代码和request_id发送到Nexmo以验证号码
我想使用firebase-queue来执行这些任务。我想询问是否有办法将任务的结果返回给客户端。我没有在firebase-queue文档中看到这样的例子。基本上,我想添加一个任务来发送手机号码到Nexmo API,并希望得到request_id作为响应,以便我可以添加另一个任务来验证用户输入的代码。
肯定有一种方法可以将响应反馈给客户端。我们在手电筒搜索集成中有一个很好的例子:
函数doSearch(index,type,query){
var ref = new Firebase(URL +'/ search') ;
var key = ref.child('request')。push({index:index,type:type,query:query})。
console.log('search',key,{index:index,type:type,query:query});
ref.child('response /'+ key).on('value',showResults);
$ b $ p
$ b 此代码在客户端JavaScript应用程序中运行,并将搜索条件调用 push()
的行中的服务器。然后等待响应返回到函数的最后一行。这里的关键在于它使用它发送请求的sam push id / key来侦听响应。这样的请求和响应匹配。
虽然Firebase Queue没有内置的握手支持,但您可以轻松地将其构建到客户端和服务器代码的应用程序。当您添加任务时,您将添加一个请求ID(来自 firebase-queue示例):
var request_id = ref.push()。key();
ref.child('queue / tasks')。push({requestId:request_id,foo:'bar'});
在您的任务工作者中,执行常规处理,然后将响应写回数据库相同的请求ID(来自 firebase-queue示例的适配器):
var ref = new Firebase('https://< your-firebase> .firebaseio.com / queue');
var responses = new Firebase('https://< your-firebase> .firebaseio.com / responses');
var queue = new Queue(ref,function(data,progress,resolve,reject){
//读取并处理任务数据
console.log(data);
//做一些工作
progress(50);
//异步完成任务
setTimeout(function(){
//写入响应到客户端
respond.child(data.requestId).set({allDone:true});
//告诉firebase-queue我们完成了
resolve();
},1000);
});
I am working on a mobile application using Firebase. In the mobile app, I need to register a user based on his/her mobile number similar to Whatsapp. I intend to use Nexmo to verify user's mobile number. There are two steps involved in verifying the number.
- Send the the user mobile number to the Nexmo API and it returns back with a request_id on success and it also sends a code to user's mobile via SMS
- Send the code and the request_id to the Nexmo to verify the number
I want to use firebase-queue to execute these tasks. I want to ask if there is a way to return results for a task back to the client. I didn't see any such example on the firebase-queue documentation. Basically, I want to add a task to send the mobile number to the Nexmo API and want to get the request_id in response so that I can add another task to verify the code entered by the user.
解决方案 There definitely is a way to get responses back to the client. We have a good example of that in the Flashlight search integration:
function doSearch(index, type, query) {
var ref = new Firebase(URL+'/search');
var key = ref.child('request').push({ index: index, type: type, query: query }).key();
console.log('search', key, { index: index, type: type, query: query });
ref.child('response/'+key).on('value', showResults);
}
This code runs in the client-side JavaScript application and sends a search term to the server in the line that calls push()
. It then "waits" for a response to come back on the last line of the function. The key here is that it listens for a response with the sam push id/key that it used to send the request. That way the request and response match up.
While Firebase Queue doesn't have built-in support for such a "handshake", you can easily build it yourself into the client and server code of your app. When you add a task, you add a request id (adapter from the firebase-queue sample):
var request_id = ref.push().key();
ref.child('queue/tasks').push({ requestId: request_id, foo: 'bar' });
In your task worker, you perform your usual processing and then write the response back into the database with the same request id (adapter from the firebase-queue sample):
var ref = new Firebase('https://<your-firebase>.firebaseio.com/queue');
var responses = new Firebase('https://<your-firebase>.firebaseio.com/responses');
var queue = new Queue(ref, function(data, progress, resolve, reject) {
// Read and process task data
console.log(data);
// Do some work
progress(50);
// Finish the task asynchronously
setTimeout(function() {
// write the response to the client
responses.child(data.requestId).set({ allDone: true });
// tell firebase-queue that we're done
resolve();
}, 1000);
});
这篇关于使用firebase-queue返回任务结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!