Answer the question
In order to leave comments, you need to log in
What is the best way to build an application architecture on Laravel + Broadcasting?
I am developing an application on Laravel, where there will be chats, correspondence, notifications...
I will use Broadcasting ( redis ,
laravel -echo-server , socket.io ) to broadcast data to clients
questions arose:
1) how to properly organize channels, broadcast name, Laravel events?
If, for example, notifications about new messages (new application, unread messages), events in chats, events in discussions work on the site - should I make a separate channel for everything?
2) How, for example, to send data only to users of a certain group, such as a chat? How to define that this user needs to send the message, even in a day...?
Answer the question
In order to leave comments, you need to log in
1. Channel for specific data. for example channels:
2. Channel for specific dialogue:
Broadcast::channel('chat.{roomId}', function ($user, $roomId) {
if ($user->canJoinRoom($roomId)) { // Проверяем, разрешено ли пользователю входить в этот чат
return ['id' => $user->id, 'name' => $user->name]; // Если да, возвращаем результат
}
});
Echo.join(`chat.${roomId}`)
.here((users) => {
console.log("В этой комнате: ", users);
})
.joining((user) => {
console.log("К нам присоединился: ", user.name);
})
.leaving((user) => {
console.log("Ушел от нас: ", user.name);
});
Broadcast::channel('chat.{chatId}', function ($user, $chatId) {
return $user->hasChat($chatId); // Проверяем состоит ли пользователь в чате
});
Echo.private(`chat.${chatId}`)
.here((users) => {
console.log("В этой комнате: ", users);
})
.joining((user) => {
console.log("К нам присоединился: ", user.name);
})
.leaving((user) => {
console.log("Ушел от нас: ", user.name);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question