Laravel 广播不写入 redis 但直接调用 redis 有效
Laravel Broadcasting does't write to redis but direct call to redis works
我尝试了 sereval tuto 来尝试设置 Laravel Redis NodeJs Socket。
它不起作用。
到目前为止:
- 正如我在 Laravel 日志中看到的那样,我的广播开始了
- 我的 redis 服务器工作(我在我的家庭控制器中添加了对 Redis 的调用,我可以在日志和 redis-cli 监视器中看到该值 - 我还安装了 Horizon,我可以在 Redis-cli 监视器中看到它的调用)
但是当我的广播触发时,我在 redis-cli 监视器中看不到任何东西
我已经尝试过显而易见的事情(缓存/清除配置/重启/...)
非常感谢您的帮助!
我的家庭控制器中的 Redis 调用:工作
Redis::set("foo","bar");
$value = Redis::get("foo");
Log::debug('Redis value :'.$value);
.环境
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=siview
DB_USERNAME=root
DB_PASSWORD=
QUEUE_DRIVER=sync
QUEUE_CONNECTION=sync
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
BROADCAST_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Database.php
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => 60,
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
'pubsub' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 2,
],
],
Boadcasting.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Broadcaster
|--------------------------------------------------------------------------
|
| This option controls the default broadcaster that will be used by the
| framework when an event needs to be broadcast. You may set this to
| any of the connections defined in the "connections" array below.
|
| Supported: "pusher", "redis", "log", "null"
|
*/
'default' => env('BROADCAST_DRIVER', 'redis'),
/*
|--------------------------------------------------------------------------
| Broadcast Connections
|--------------------------------------------------------------------------
|
| Here you may define all of the broadcast connections that will be used
| to broadcast events to other systems or over websockets. Samples of
| each available type of connection are provided inside this array.
|
*/
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'pubsub',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;
class SendMessageEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
private $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('App.User.' . $this->user->id);
}
public function broadcastWith()
{
return ['message' => 'Salut ' . $this->user->name];
}
}
路由通道
<?php
/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/
Broadcast::channel('App.User.{id}', function ($user, $id) {
return true;//(int) $user->id === (int) $id;
});
路由网络
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::get('/send/{user}', function(\App\User $user){
event(new \App\Events\SendMessageEvent($user));
});
Route::get('/home', 'HomeController@index')->name('home');
Laravel.log 显示我的广播
[2018-11-04 09:39:42] local.INFO: Broadcasting [App\Events\SendMessageEvent] on channels [private-App.User.1] with payload:
{
"message": "Salut nathalie",
"socket": null
}
redis-cli 监视器显示我的家庭控制器测试
1541324518.159040 [0 127.0.0.1:59334] "SELECT" "0"
1541324518.159776 [0 127.0.0.1:59334] "SET" "foo" "bar"
1541324518.160360 [0 127.0.0.1:59334] "GET" "foo"
我找到了,有点奇怪:
我正在使用 wamp windows。
wamp 服务器不是由 Laravel 服务器启动的。
奇怪的是,一切似乎都运行良好(没有错误,页面已呈现,身份验证正在运行...)
我刚开始 laravel 使用:php artisan serve
现在我可以在 Redis-cli 监视器中看到 :
1541328214.873429 [0 127.0.0.1:52962] "SELECT" "2"
1541328214.874260 [2 127.0.0.1:52962] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\Events\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
1541328661.366923 [0 127.0.0.1:53078] "SELECT" "2"
1541328661.367643 [2 127.0.0.1:53078] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\Events\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
1541328679.554077 [0 127.0.0.1:53083] "SELECT" "2"
1541328679.555197 [2 127.0.0.1:53083] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\Events\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
我发现了同样的问题,经过大量无用的研究后,更新 de redis 服务器非常简单。
在此处下载:
https://redis.io/download
或此处 windows:
https://redislabs.com/ebook/appendix-a/a-3-installing-on-windows/a-3-2-installing-redis-on-window/
确保你有最新版本的redis serve。
Laravel 使用 redis 服务器最新版本支持的 eval
之类的命令。
我用了 Redis 3.2.100
问题解决了。
我尝试了 sereval tuto 来尝试设置 Laravel Redis NodeJs Socket。
它不起作用。
到目前为止: - 正如我在 Laravel 日志中看到的那样,我的广播开始了 - 我的 redis 服务器工作(我在我的家庭控制器中添加了对 Redis 的调用,我可以在日志和 redis-cli 监视器中看到该值 - 我还安装了 Horizon,我可以在 Redis-cli 监视器中看到它的调用)
但是当我的广播触发时,我在 redis-cli 监视器中看不到任何东西
我已经尝试过显而易见的事情(缓存/清除配置/重启/...)
非常感谢您的帮助!
我的家庭控制器中的 Redis 调用:工作
Redis::set("foo","bar");
$value = Redis::get("foo");
Log::debug('Redis value :'.$value);
.环境
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=siview
DB_USERNAME=root
DB_PASSWORD=
QUEUE_DRIVER=sync
QUEUE_CONNECTION=sync
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
BROADCAST_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Database.php
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => 60,
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
'pubsub' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 2,
],
],
Boadcasting.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Broadcaster
|--------------------------------------------------------------------------
|
| This option controls the default broadcaster that will be used by the
| framework when an event needs to be broadcast. You may set this to
| any of the connections defined in the "connections" array below.
|
| Supported: "pusher", "redis", "log", "null"
|
*/
'default' => env('BROADCAST_DRIVER', 'redis'),
/*
|--------------------------------------------------------------------------
| Broadcast Connections
|--------------------------------------------------------------------------
|
| Here you may define all of the broadcast connections that will be used
| to broadcast events to other systems or over websockets. Samples of
| each available type of connection are provided inside this array.
|
*/
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'pubsub',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;
class SendMessageEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
private $user;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('App.User.' . $this->user->id);
}
public function broadcastWith()
{
return ['message' => 'Salut ' . $this->user->name];
}
}
路由通道
<?php
/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/
Broadcast::channel('App.User.{id}', function ($user, $id) {
return true;//(int) $user->id === (int) $id;
});
路由网络
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::get('/send/{user}', function(\App\User $user){
event(new \App\Events\SendMessageEvent($user));
});
Route::get('/home', 'HomeController@index')->name('home');
Laravel.log 显示我的广播
[2018-11-04 09:39:42] local.INFO: Broadcasting [App\Events\SendMessageEvent] on channels [private-App.User.1] with payload:
{
"message": "Salut nathalie",
"socket": null
}
redis-cli 监视器显示我的家庭控制器测试
1541324518.159040 [0 127.0.0.1:59334] "SELECT" "0"
1541324518.159776 [0 127.0.0.1:59334] "SET" "foo" "bar"
1541324518.160360 [0 127.0.0.1:59334] "GET" "foo"
我找到了,有点奇怪:
我正在使用 wamp windows。
wamp 服务器不是由 Laravel 服务器启动的。
奇怪的是,一切似乎都运行良好(没有错误,页面已呈现,身份验证正在运行...)
我刚开始 laravel 使用:php artisan serve 现在我可以在 Redis-cli 监视器中看到 :
1541328214.873429 [0 127.0.0.1:52962] "SELECT" "2"
1541328214.874260 [2 127.0.0.1:52962] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\Events\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
1541328661.366923 [0 127.0.0.1:53078] "SELECT" "2"
1541328661.367643 [2 127.0.0.1:53078] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\Events\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
1541328679.554077 [0 127.0.0.1:53083] "SELECT" "2"
1541328679.555197 [2 127.0.0.1:53083] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\Events\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
我发现了同样的问题,经过大量无用的研究后,更新 de redis 服务器非常简单。
在此处下载: https://redis.io/download
或此处 windows: https://redislabs.com/ebook/appendix-a/a-3-installing-on-windows/a-3-2-installing-redis-on-window/
确保你有最新版本的redis serve。
Laravel 使用 redis 服务器最新版本支持的 eval
之类的命令。
我用了 Redis 3.2.100
问题解决了。