У меня есть приложение laravel, где я использую события для уведомления клиентов об обновлениях. Раньше я использовал pusher для трансляции этих событий, но я хочу изменить это на redis (или что-то другое без необходимости использования сторонних разработчиков, таких как pusher). Я попытался использовать predis и отправить сообщение, подобное следующему:
$redis = Redis::connection();
$redis->publish('message', 'test message');
Это работает, но я хотел бы транслировать события типа: event(new TestEvent())
и получать их на моем сервере узлов через socket.io/redis. / * и др * 1016. Что мне нужно изменить, чтобы сделать это?
.env
BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Настройка трансляции:
<?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', 'null'),
/*
|--------------------------------------------------------------------------
| 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'),
// 'useTLS' => env('PUSHER_APP_USE_TLS'),
'host' => env('PUSHER_APP_HOST'),
'port' => env('PUSHER_APP_PORT'),
'scheme' => env('PUSHER_APP_SCHEME')
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
Мой сервер узлов:
const express = require('express');
const app = express();
const http = require('http');
const server = http.Server(app);
const socketIO = require('socket.io');
const io = socketIO(server);
const redis = require('redis');
const port = 3000;
io.on('connection' , (socket) => {
console.log('user connected');
const redisClient = redis.createClient();
redisClient.subscribe('message');
redisClient.on('message', function(channel, message) {
console.log(channel);
console.log(message);
socket.emit(channel, message);
});
socket.on('test1', (message) => {
console.log(message);
});
});
server.listen(port, () => {
console.log('started on port: ' + port);
});