I have a messenger app set up where there is a list of chat rooms. When someone clicks one from the list, the site removes the original conversation and loads up the new conversation. In Phoenix, I want the user to leave the original channel/topic and then join the new channel/topic. The problem I’ve discovered, is that when the user sends a message in the new conversation, it ends up going to the previous conversation as well. It doesn’t seem like convchannel.leave()
is working. Here is my client-side Javascript:
let socket = new Phoenix.Socket("wss://app.example.com/socket", {params: () => {return {token: window.dbchattoken, anses: window.userid, at: window.at};},});
socket.connect();
let convchannel = null; # conversation channel doesn't exist on page load until user clicks first room.
let convpresence = null;
function loadConversation(roomid) { # when a user clicks on a room, load up conversation.
# ... remove old conversation / load up new conversation on the page.
if(convchannel !== null) { # if convchannel is initialized, leave existing room before joining the new one.
console.log("leave channel");
convchannel.leave();
}
convchannel = socket.channel("conv:"+roomid+"", {});
convpresence = new Phoenix.Presence(convchannel);
convchannel.join()
.receive("ok", resp => { console.log("Joined conversation successfully", resp); })
.receive("error", resp => { console.log("Error ", resp); })
convchannel.on("receiveMessage", (payload) => {console.log("receive message: ", payload); displayMessage(roomid,payload); });
}
I’m still sort of new to Phoenix, so I’m wondering if this is supposed to be handled in a different manner.
Thanks