Hi,
I’m trying to create an app which features direct messaging between two users. The users are randomly assigned; you hit a button and you’re randomly placed in a chat with another active user.
I’ve read a few threads about this and think I get the gist of what to do, but I’m struggling to implement. Here’s what I have so far:
First of all, every user joins their own personal channel, which references their ID:
const personalChannel = socket.channel(`chat:${userAID}`);
personalChannel.join();
Then, when a user hits the ‘start a chat’ button I make an ajax request to get the ID of a random user, and push a message containing this user’s ID:
personalChannel.push("initiate_convo", {
id: toUser
});
In Elixir, I attempt to use that message to broadcast to the target user’s channel:
def handle_in("initiate_convo", %{"id" => id}, socket) do
broadcast!("chat:#{id}", "open_convo", %{
from: "16",
room_topic: "chat:16:#{id}"
})
{:reply, {:ok, %{}}, socket}
end
Finally, I catch that on the client side, where I intend to open a private channel for the two users:
personalChannel.on(
"open_convo",
({ from: remoteUserID, room_topic: topic }) => {
let privRoom = socket.channel(topic);
privRoom.join();
}
);
At the moment I get the following error when hitting the ‘start a chat’ button:
(FunctionClauseError) no function clause matching in Phoenix.Channel.assert_joined!/1
(phoenix) lib/phoenix/channel.ex:589: Phoenix.Channel.assert_joined!("chat:12")
But even beyond that, I’m struggling to get to grips with whether my approach is conceptually sound, or whether I’m missing anything?
Thank you!