如何:在 Laravel 4 中使用 Iron.io MQ、推送队列和 AJAX 获取实时通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20129033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:40:42  来源:igfitidea点击:

HOW TO: Get real-time notifications in Laravel 4 using Iron.io MQ, Push Queues & AJAX

ajaxlaravelwebsocketpush-notificationqueue

提问by Jared Eitnier

I've integrated ironMQpush queues in my Laravel 4 app for longer running processes. I have some views that perform a $.getthat might take 30 seconds. I wanted to see what others are doing to easily get notified when ironMQis done pushing back to an endpoint.

ironMQ在我的 Laravel 4 应用程序中集成了推送队列,以便运行更长时间的进程。我有一些执行$.get可能需要 30 秒的视图。我想看看其他人正在做什么,以便在ironMQ完成推送回端点后轻松获得通知。

An example of what I will be doing with push queues mostly:

我将主要使用推送队列做的一个例子:

public function getCompletedTasks() {

    $user = User::find(Auth::user()->id);

    Queue::push(function($job) use ($user) {
        $recent = new Recent;
        $tasks  = $recent->getCompletedTasks($user);
        // append data from $tasks to DOM
        // here's where I want to receive my notification
    });     
}

Here I am just getting tasks from an API that match data from user.

在这里,我只是从匹配用户数据的 API 中获取任务。

I know I can store the response data to a database and use AJAX long polling to constantly check for the data but it seems like too much work for most situations I will need to do this. I don't know much about websockets. What types of things have you guys done in these situations? And if you have any examples that would be very helpful. Thanks.

我知道我可以将响应数据存储到数据库并使用 AJAX 长轮询来不断检查数据,但在大多数情况下,我需要这样做似乎工作量太大。我对 websockets 不太了解。在这些情况下,你们做了什么类型的事情?如果你有任何非常有帮助的例子。谢谢。

UPDATE: Solved the issue using Pusher. See my answer.

更新:使用 Pusher 解决了这个问题。看我的回答。

回答by Jared Eitnier

I was able to solve my problem with the help of Pusher. Here's what I did:

我能够在Pusher的帮助下解决我的问题。这是我所做的:

Setup my Iron MQ push queue as normal. In routes.php:

像往常一样设置我的 Iron MQ 推送队列。在routes.php

Route::post('queue/push', function() {
    return Queue::marshal();
});

Installed pusher laravel package.

安装了pusher laravel 包

In my controller then I Queue::pushmy data. Inside the closure I trigger a new Pusher channel. This will obviously only trigger when the data has been returned from IronMQ.

在我的控制器中,然后是Queue::push我的数据。在闭包内,我触发了一个新的 Pusher 通道。这显然只会在数据从 IronMQ 返回时触发。

public function getCompletedTasks() {

    $user = User::find(Auth::user()->id);

    Queue::push(function($job) use ($user) {

        $recent = new Recent;
        $tasks  = $recent->getCompletedTasks($user);

        $pusher = new Pusher('xxx', 'xxx', 'xxx');
        $pusher->trigger('reports', 'get_completed_tasks', array('tasks' => $tasks));

        $job->delete();
    });
});

Next in my view I call my AJAX function with no callback since I won't be doing anything else just yet:

在我看来,接下来我将不带回调调用我的 AJAX 函数,因为我还不会做任何其他事情:

$.get('account/tasks/completed');

Next in my view I initialize Pusher, subscribe to the event and bind get_completed_tasksto the Pusher channel. Now we just wait for a response from Pusher which will then allow me to perform the latter part of my original AJAX request:

在我看来,接下来我初始化 Pusher,订阅事件并绑定get_completed_tasks到 Pusher 通道。现在我们只需等待 Pusher 的响应,然后我就可以执行原始 AJAX 请求的后半部分:

{{ HTML::script('//js.pusher.com/2.1/pusher.min.js') }}
<script>
var pusher  = new Pusher('xxx');
var channel = pusher.subscribe('reports');
    channel.bind('get_completed_tasks', function(data) {
        // do something with the data returned
    });
</script>

Once I used Pusher in my app, the rest was a breeze. Hope this helps someone!

一旦我在我的应用程序中使用了 Pusher,剩下的就变得轻而易举了。希望这对某人有帮助!