I’m trying to create a channel that will allow users to exchange information privately. According to some of the other threads on the topic this can be done by creating individual channel topics for users and sending messages between them using socket.endpoint.broadcast!/3
. I’m trying to do this but I can’t get it to work.
This is the channel.ex in question.
defmodule ChatWeb.PrivateChannel do
use ChatWeb, :channel
require Logger
alias ChatWeb.Presence
alias Chat.Repo
alias Chat.Auth.User
def join("private:" <> user_id, _message, socket) do
{:ok, %{channel: "private:#{user_id}"}, assign(socket, :user_id, user_id)}
end
#This one is mean to receive the message from the sender
def handle_in("offer", %{"message" => message, "sender" => sender, "recipient" => recipient}, socket) do
Logger.info "THIS" # I just use these to check if the function has fired or not
broadcast!(socket, "private:#{recipient}:message", %{message: message, sender: sender})
{:noreply, socket}
end
#This is meant take the message from the client side and sends it to the recipient
def handle_in("message", %{"message" => message, "recipient" => recipient, "sender" => sender}, socket) do
Logger.info "THAT"
socket.endpoint.broadcast!("private:#{recipient}", "offer", %{message: message, sender: sender, recipient: recipient})
{:noreply, socket}
end
def id(_socket), do: nil
end
I’m not getting any errors. It’s just not doing anything and I’m not sure why.