Type mismatch for @callback handle_in/3 in Phoenix.Channel behaviour

Try learning again Phoenix channels and stumbled upon a dialyzer warning I can’t seem to solve.
My whole channel code below.

  @impl true
  def join("room:lobby", payload, socket) do
    send(self(), :after_join)

    {:ok, assign(socket, current_user: payload)}
  end

  @impl true
  def handle_info(:after_join, socket) do
    {:ok, _} =
      Presence.track(socket, socket.assigns.current_user.id, %{
        nickname: socket.assigns.current_user.nickname,
        online_at: inspect(System.system_time(:second))
      })

    messages = MessageView.render("messages.json", %{messages: Chat.list_messages()})

    push(socket, "presence_state", Presence.list(socket))
    push(socket, "list_messages", messages)

    {:noreply, socket}
  end

  @impl true
  def handle_in("create_message", payload, socket) do
    {:ok, user} = Accounts.get_user!(socket.current_user.id)
    {:ok, message} = Chat.create_message(user, payload)

    message = MessageView.render("message.json", %{message: message})
    broadcast(socket, "message_created", message)
    {:noreply, socket}
  end

Dialyzer warnings:

The inferred type for the 3rd argument is not a
supertype of the expected type for the handle_in/3 callback
in the Phoenix.Channel behaviour.

Success type:
%Phoenix.Socket{:current_user => atom() | %{:id => _, _ => _}, :joined => true, _ => _}

Behaviour callback type:
%Phoenix.Socket{
  :assigns => map(),
  :channel => atom(),
  :channel_pid => pid(),
  :endpoint => atom(),
  :handler => atom(),
  :id => nil | binary(),
  :join_ref => _,
  :joined => boolean(),
  :private => %{},
  :pubsub_server => atom(),
  :ref => _,
  :serializer => atom(),
  :topic => binary(),
  :transport => atom(),
  :transport_pid => pid()
}
Type mismatch for @callback handle_in/3 in Phoenix.Channel behaviour.

Expected type:

  {:noreply,
   %Phoenix.Socket{
     :assigns => map(),
     :channel => atom(),
     :channel_pid => pid(),
     :endpoint => atom(),
     :handler => atom(),
     :id => nil | binary(),
     :join_ref => _,
     :joined => boolean(),
     :private => %{},
     :pubsub_server => atom(),
     :ref => _,
     :serializer => atom(),
     :topic => binary(),
     :transport => atom(),
     :transport_pid => pid()
   }}
  | {:noreply,
     %Phoenix.Socket{
       :assigns => map(),
       :channel => atom(),
       :channel_pid => pid(),
       :endpoint => atom(),
       :handler => atom(),
       :id => nil | binary(),
       :join_ref => _,
       :joined => boolean(),
       :private => %{},
       :pubsub_server => atom(),
       :ref => _,
       :serializer => atom(),
       :topic => binary(),
       :transport => atom(),
       :transport_pid => pid()
     }, :hibernate | timeout()}
  | {:reply, atom() | {atom(), map()},
     %Phoenix.Socket{
       :assigns => map(),
       :channel => atom(),
       :channel_pid => pid(),
       :endpoint => atom(),
       :handler => atom(),
       :id => nil | binary(),
       :join_ref => _,
       :joined => boolean(),
       :private => %{},
       :pubsub_server => atom(),
       :ref => _,
       :serializer => atom(),
       :topic => binary(),
       :transport => atom(),
       :transport_pid => pid()
     }}
  | {:stop, _,
     %Phoenix.Socket{
       :assigns => map(),
       :channel => atom(),
       :channel_pid => pid(),
       :endpoint => atom(),
       :handler => atom(),
       :id => nil | binary(),
       :join_ref => _,
       :joined => boolean(),
       :private => %{},
       :pubsub_server => atom(),
       :ref => _,
       :serializer => atom(),
       :topic => binary(),
       :transport => atom(),
       :transport_pid => pid()
     }}
  | {:stop, _, atom() | {atom(), map()},
     %Phoenix.Socket{
       :assigns => map(),
       :channel => atom(),
       :channel_pid => pid(),
       :endpoint => atom(),
       :handler => atom(),
       :id => nil | binary(),
       :join_ref => _,
       :joined => boolean(),
       :private => %{},
       :pubsub_server => atom(),
       :ref => _,
       :serializer => atom(),
       :topic => binary(),
       :transport => atom(),
       :transport_pid => pid()
     }}


Actual type:

  {:noreply,
   %Phoenix.Socket{:current_user => atom() | %{:id => _, _ => _}, :joined => true, _ => _}}
1 Like

according to your usage dialyzer thinks that socket has a current_user field.

{:ok, user} = Accounts.get_user!(socket.current_user.id) 

current_user is in socket.assigns.

1 Like

Yeah, I already fixed it a while a go but forgot to update the thread.

1 Like