Phoenix websocket: how to send message to one user

okay :slight_smile:

1 Like

I have a Notifier acting like a gateway between the chat system and Phoenix.

Whenever a user receive a message, it will be notified like this…

defmodule AppApiWeb.Notifier do
  require Logger
  alias AppApiWeb.Endpoint

  def notify(%{payload: %{from: from, to: to}, type: :ping}) do
    Endpoint.broadcast!("user:#{to}", "ping", %{from: from})
  end

  def notify(message) do
    Logger.debug(fn -> "Unknown notification #{inspect(message)}" end)
  end
end

Only the user of the channel can receive the message.

Here it is just a ping, but You can add a message if You want.

okay bro