event broadcasting redis
Hello guys, how are you? I'm setting up a local redis / predis server with Node and Lumen, follow the codes below:
bootstrap/app.php
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(Illuminate\Mail\MailServiceProvider::class);
$app->register(Illuminate\Redis\RedisServiceProvider::class);
//$app->register(App\Providers\BroadcastServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
app/providers/EventServiceProvider.php
<?php
namespace App\Providers;
use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\LoginEvent' => [
'App\Listeners\LoginListener',
],
];
protected $subscribe = [
'App\Listeners\LoginListener',
];
}
app/events/LoginEvent.php
<?php
namespace App\Events;
use App\Usuarios;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Broadcasting\Channel;
class LoginEvent implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $usuario;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Usuarios $usuario)
{
$this->usuario = $usuario;
}
/**
* Determine if events and listeners should be automatically discovered.
*
* @return bool
*/
public function broadcastOn()
{
return new Channel('user');
}
public function broadcastAs()
{
return 'my-event';
}
}
app/listeners/LoginListener.php
<?php
namespace App\Listeners;
use App\Events\LoginEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class LoginListener implements ShouldQueue
{
use InteractsWithQueue;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param \App\Events\LoginEvent $event
* @return void
*/
public function handle(LoginEvent $event)
{
//
}
public function failed(LoginEvent $event, $exception)
{
//
}
public function subscribe($events)
{
//
}
}
To fire the event in the controller:
<?php
namespace App\Http\Controllers;
use App\Usuarios;
use Illuminate\Http\Request;
use Tymon\JWTAuth\JWTAuth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use App\Mail\RecoverPass;
use App\Events\LoginEvent;
class LoginController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
protected $jwt;
public function __construct(JWTAuth $jwt)
{
$this->jwt = $jwt;
}
public function Login(Request $request)
{
$this->validate($request, [
'email' => 'required|email|max:255',
'password' => 'required'
]);
if (!$token = $this->jwt->claims(['email' => $request->email])->attempt($request->only('email', 'password'))) {
return response()->json([
'user' => ['Usuário não encontrado.']
]
, 401);
}
// register event
event(new LoginEvent($request->user()));
return response()->json(compact('token'));
}
[...]
This is the laravel-echo-server.json configuration:
{
"authHost": "http://localhost",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "e532e73d94855a51",
"key": "8f5b3c066ac9772904f9dbf574457832"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"host": "127.0.0.1",
"port": 6379
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "*",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
After the configuration I started the server with the command:
npx laravel-echo-server start
When I trigger the event through the LoginController controller, the message reaches the redis service and on the laravel-echo-server, redis-cli:
127.0.0.1:6379> MONITOR
OK
1585685578.597719 [0 127.0.0.1:49958] "info"
1585685578.598525 [0 127.0.0.1:49959] "info"
1585685578.600684 [0 127.0.0.1:49959] "psubscribe" "*"
1585685579.890517 [0 127.0.0.1:49963] "SELECT" "0"
1585685579.890846 [0 127.0.0.1:49963] "EVAL" "for i = 2, #ARGV do\n redis.call('publish', ARGV[i], ARGV[1])\nend" "0" "{\"event\":\"my-event\",\"data\":{\"usuario\":{\"id\":1,\"name\":\"Usu\u00e1rios Teste 01\",\"email\":\"[email protected]\",\"status\":\"A\",\"verificado\":1,\"created_at\":\"2020-03-11 10:19:44\",\"updated_at\":\"2020-03-26 16:14:26\"},\"socket\":null},\"socket\":null}" "user"
1585685579.891000 [0 lua] "publish" "user" "{\"event\":\"my-event\",\"data\":{\"usuario\":{\"id\":1,\"name\":\"Usu\u00e1rios Teste 01\",\"email\":\"[email protected]\",\"status\":\"A\",\"verificado\":1,\"created_at\":\"2020-03-11 10:19:44\",\"updated_at\":\"2020-03-26 16:14:26\"},\"socket\":null},\"socket\":null}"
And in the laravel-echo-server:
$ npx laravel-echo-server start
L A R A V E L E C H O S E R V E R
version 1.6.1
⚠ Starting server in DEV mode...
✔ Running at localhost on port 6001
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
Channel: user
Event: my-event
However, when I make a request to list the channels, there is no channel: Image Postman
Am I doing something wrong?
Edit:
When I call the event URL: Run Events Postman
In laravel-echo-server: Erro laravel-echo-server
Thanks :)
Please or to participate in this conversation.