问题
About
I am learning Vue.js with Laravel. Presently, practicing on sending real time text messages between 2 users.
Below code sends message to another user
var url = "http://localhost:6001/apps/My_appId/events?auth_key=My_Key";
var socketId = Echo.socketId();
var request = {
"channel": "private-Send-Message-Channel.2",
"name": "MessengerEvent",
"data": {
"msg": message
},
"socket_id": socketId
};
axios.post(url,
JSON.stringify(request))
.then((response) => {
//Success callback
}
);
Receive the message
window.Echo.private('Send-Message-Channel.2')
.on('MessengerEvent', (e) => {
//Receive the message
}
);
Question: Is there any callback for on leave, on join , on typing, total users?
回答1:
You need to user a presence
channel. As described these channels are also private:
All presence channels are also private channels
You can set up a presence channel by using .join
.
Echo.join(`chat.${roomId}`)
.here((users) => {
//
})
.joining((user) => {
console.log(user.name);
})
.leaving((user) => {
console.log(user.name);
});
There you have access to the callback that you are looking for.
来源:https://stackoverflow.com/questions/48868541/is-there-any-callback-for-on-leave-on-join-on-typing-in-vue-js