peppy

peppy

How to properly leave a channel and join another channel from client-side? (or leave topic -> join topic)

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

First Post!

peppy

peppy

Sorry, nevermind. After banging my head against the wall for hours, I found that the subtle issue was related to Microsoft Edge. Apparently, Javascript runs a little differently on Edge vs Chrome, so a small error elsewhere in my script was throwing off Phoenix. Made it even more confusing since the “leave room” event was refusing to run only some of the time (only when I tested on Edge), which threw off everything in Chrome too…

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

We're in Beta

About us Mission Statement