V
V
Vasyl Fomin2018-01-24 15:46:54
Broadcast
Vasyl Fomin, 2018-01-24 15:46:54

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 answer(s)
T
Taras Fomin, 2018-01-24
@fomvasss

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);
    });

Read more: https://laravel.com/docs/5.5/broadcasting#authoriz...
UPD: You can also use private channels:
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);
    });

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question