Skip to content

Instantly share code, notes, and snippets.

@ghaffaru
Last active November 25, 2021 09:19
Show Gist options
  • Select an option

  • Save ghaffaru/34a038328d6032b5d0462a307cb69d3e to your computer and use it in GitHub Desktop.

Select an option

Save ghaffaru/34a038328d6032b5d0462a307cb69d3e to your computer and use it in GitHub Desktop.
// Connecting using laravel echo
Echo = new Echo({
broadcaster: "pusher",
key: "74bf7b405754182ff555",
cluster: "mt1",
namespace: "",
encrypted: true,
authEndpoint: "https://api.africanobservations.com/broadcasting/auth",
auth: {
headers: {
Authorization: "Bearer " + loggedInUserAccessToken,
},
},
});
//1. Listening to a new message event on the screen where user is chatting an adviser
Echo.private(`user_adviser_chat.${engagement.id}`)
.listen('UserChatAdviserEvent', e => {
// push new message
messages.push(e.chat.message)
// e structure is
/*
{
"chat": {
"message": "Hello adviser, 1",
"adviser_profile_id": 1,
"user_id": 1,
"sender": "user",
"receiver": "adviser",
"adviser_engagement_id": 1,
"updated_at": "2021-10-30T13:20:19.000000Z",
"created_at": "2021-10-30T13:20:19.000000Z",
"id": 8
}
}
*/
});
// Getting adviser online status
Echo.join('adviser_online')
.here(advisers => {
// loop and set the adviser's online status to true
this.allAdvisers.forEach(f => {
advisers.forEach(adviser => {
if (adviser.id === f.id) f.online = true;
})
})
})
.joining(adviser => {
// set the adviser who just came online to true (showing green)
let adviser = this.allAdvisers.find(f => f.id === adviser.id)
friend.online = true;
})
.leaving(adviser => {
// remove online status of the adviser who just left
let adviser = this.allAdvisers.find(f => f.id === adviser.id)
friend.online = false;
})
// Check if adviser is typing
Echo.private(`chat.${engagement_id}`)
.listenForWhisper('typing', e => {
this.isTyping = true;
setTimeout(() => {
this.isTyping = false;
}, 2000);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment