// server.js (node server/server.js)
let io = require('socket.io')(6001);
let Redis = require('ioredis');
let redis = new Redis();
redis.psubscribe('*', function(error, count) {
});
redis.on('chat', function(channel, message) {
console.log(channel, message);
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
// После добавления коммента
addComment() {
axios.post('/api/comments/anime/add', {
comment: this.textarea
}).then((response) => {
});
let socket = io('http://anime-music.ru:6001');
socket.on('comments', function(data) {
console.log(data);
});
}
// Event newCommentAdded
<?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\Models\AnimeComment;
class NewCommentAdded implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $comment;
/**
* Create a new event instance.
*
* @param AnimeComment $comment
*/
public function __construct(AnimeComment $comment)
{
$this->comment = $comment;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new Channel('chat');
}
public function broadcastAs()
{
return ['comments'];
}
}
// Routers/Channels.php
Broadcast::channel('chat', function ($comment) {
return [
'hello' => 'hello',
'info' => 'info'
];
});