Answer the question
In order to leave comments, you need to log in
How to listen to 2 events on the same channel?
Goodnight! There is a chat in which receiving messages and a list of online users is performed via Socket.io.
Event code:
class ChatOnline implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
$this->dontBroadcastToCurrentUser();
}
public function broadcastOn()
{
return new PresenceChannel('chat');
}
}
Echo.join('chat')
.here((users) => {
$.each(users, function(index, value){
$('#chat-online').append('<div id="user-' + value.id + '">' + value.name + '</div>');
});
})
.joining((user) => {
$('#chat-online').append('<div id="user-' + user.id + '">' + user.name + '</div>');
})
.leaving((user) => {
$('#user-' + user.id).remove();
}).listen('ChatOnline', (data) => {
switch (data.data.type) {
case 1: Выполняется если приходит сообщение; break;
case 2: Выполняется если было удалено сообщение; break;
});
Answer the question
In order to leave comments, you need to log in
One event equals one action.
type in this case is shit code, since there is no reason to use it, rather than separate events (which are easier to maintain, which can be typed without pain, etc.).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question