【问题标题】:Automatically trigger broadcast event in laravel 5.2laravel 5.2 自动触发广播事件
【发布时间】:2016-10-03 08:29:39
【问题描述】:

我正在使用 BROADCAST_DRIVER=redis 来触发 laravel 5.2 事件。 一旦我在命令提示符下运行以下服务:

  1. 在第一个选项卡中,运行 node socket.js

    您应该会看到“正在侦听端口 3000”

  2. 在第二个选项卡中运行 redis-server --port 3001

之后并排打开两个浏览器窗口,在第一个窗口中点击 URL:“http://your-project-name.app/fire

第二个:“http://your-project-name.app/test

继续刷新第一个窗口,您应该会看到第二页的内容已更新。

但我不想刷新页面,我只想在后台触发广播事件,也不想运行服务“node socket.js and redis-server --port 3004”。

我已经安装了 node、redis、express ioredis socket.io 并创建了事件。

我的套接字代码:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel', function(err, count) { });
redis.on('message', function(channel, message) { 
  console.log('Message Recieved: ' + message);
  message = JSON.parse(message);
  io.emit(channel + ':' + message.event, message.data);
});

http.listen(3004, function(){
  console.log('Listening on Port 3004');
});

【问题讨论】:

  • 我的 socket.js 代码是 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var Redis = require('ioredis'); var redis = new Redis(); redis.subscribe('test-channel', function(err, count) { }); redis.on('message', function(channel, message) { console.log('Message Recieved: ' + message); message = JSON.parse(message); io.emit(channel + ':' + message.event , 消息. 数据); }); http.listen(3004, function(){ console.log('Listening on Port 3004'); });

标签: node.js laravel redis socket.io


【解决方案1】:

你需要使用Redis Pub/Sub

这些 Redis 命令允许您监听给定“通道”上的消息。您可以将消息从另一个应用程序发布到通道,甚至使用另一种编程语言,允许应用程序/进程之间轻松通信。

首先,让我们通过 Redis 使用 subscribe 方法在频道上设置一个监听器。我们将把这个方法调用放在 Artisan 命令中,因为调用 subscribe 方法会开始一个长时间运行的过程:

    <?php

    namespace App\Console\Commands;

    use Redis;
    use Illuminate\Console\Command;

    class RedisSubscribe extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'redis:subscribe';

        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Subscribe to a Redis channel';

        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            Redis::subscribe(['test-channel'], function($message) {
                echo $message;
            });
        }
    }

转到终端,在您的根文件夹中启动命令

 php artisan redis:subscribe

现在,我们可以使用 publish 方法向频道发布消息:

 Redis::publish('test-channel', json_encode(['foo' => 'bar']));

此方法不使用nodejs。

【讨论】:

  • 对于接收端我使用以下代码:: 还有我在 laravel 5.2 中有事件代码,接下来我需要做什么,你能给我一些指导吗?急切地等待你的宝贵回应!!
  • 我的问题是在特定时间间隔内自动触发后台事件。我一切正常,但是当我手动触发它时。
  • Pub/sub 是你需要的,忘了 nodejs。在laravel.com/docs/5.2/redis#pubsub 你有这个例子。首先创建一个命令来监听,然后使用 Redis::publish() 将消息发送到通道
  • 如果你想在后台触发它,你一定不要使用nodejs,因为它只是在你的前端触发事件,而不是在后台。改变我给你看的方法
  • 我会在未来实施您的解决方案并通知您,感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
相关资源
最近更新 更多