Node.js Laravel通过redis向节点服务器广播事件
我有一个laravel应用程序,我使用事件通知客户端更新。以前我使用pusher来广播这些事件,但我想把它改成redis(或者任何不需要第三方pusher的东西)。我尝试使用predis并发送如下消息:Node.js Laravel通过redis向节点服务器广播事件,node.js,laravel,redis,socket.io,predis,Node.js,Laravel,Redis,Socket.io,Predis,我有一个laravel应用程序,我使用事件通知客户端更新。以前我使用pusher来广播这些事件,但我想把它改成redis(或者任何不需要第三方pusher的东西)。我尝试使用predis并发送如下消息: $redis = Redis::connection(); $redis->publish('message', 'test message'); 这确实有效,但我希望广播事件,如:event(newtestevent()),并通过socket.io/redis/etc在节点服务器上接收
$redis = Redis::connection();
$redis->publish('message', 'test message');
这确实有效,但我希望广播事件,如:event(newtestevent())
,并通过socket.io/redis/etc在节点服务器上接收它们。要执行此操作,我必须更改哪些内容
环境署署长
广播配置:
<?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',
],
],
];
<?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);
});