getChat(id) {
this.chatLoading = true;
let currentAppointmentId = parseInt(this.$route.params.id);
// Disconnect from the current channel before pushing to the new route
if (this.chatListener) {
window.Echo.leaveChannel(`chat${this.ConvoId}`);
this.chatListener = null;
}
axios.post("/getconvo", { appointmentId: id }).then(({ data }) => {
this.$router.push({
name: "chat",
params: { id: parseInt(id) },
});
this.chat = data;
this.$nextTick(() => this.scrollToEnd());
this.listen(id);
this.chatLoading = false;
});
},
listen(appointmentId) {
if (!this.chatListener) {
this.chatListener = (e) => {
console.log(e);
if (
Array.isArray(this.chat) &&
e.appointmentId === appointmentId
) {
this.chat.push({
chats: e.message,
userId: e.userId,
appointmentId: e.appointmentId,
});
}
};
window.Echo.private(`chat${appointmentId}`).listen(
".newchat",
this.chatListener
);
}
I'm trying to leave the channel when i click a certain conversation, because if i dont messages get mixed up when they open a diffrent convo and send messages. i tried asking chatgpt but to no luck it doesnt work, while using debug console of pusher it only subscribe to convo that aren't clicked but wont unsub when leaing that convo.also i noticed that it doesnt get mixed up if the user is subscribed to one channel at a time. which is why i want to unsubscribe a channel. How do i do this thank you for your help