peoj

peoj

Hi all, hoping one of you experts might be able to point me to the error of my ways…

I’m attempting to allow a user to send a direct message to another user via a socket. I am finding every message times out, and I’m trying to find out why!

Here’s my code:

user_socket.ex assigns a UUID to the socket, and then identifies the socket using that UUID. I am expecting that it will then be possible to reference the socket topic by user:<uuid>.
(see Phoenix.Socket — Phoenix v1.8.8)

defmodule MyAppWeb.UserSocket do
  use Phoenix.Socket

  ## Channels

  # Room channel
  channel "room:*", MyAppWeb.RoomChannel

  @impl true
  def connect(_params, socket, _connect_info) do
    {:ok, assign(socket, :user_id, UUID.uuid4())}
  end

  @impl true
  def id(socket), do: "user:#{socket.assigns.user_id}"
end

In room_channel.ex I then provide a handler for the message topic which is the one I’ve chosen for users to be able to send one another direct messages:

defmodule MyAppWeb.RoomChannel do
  require Logger

  use Phoenix.Channel
  alias MyApp.RoomPresence

  def join("room:" <> _roomId, _message, socket) do
    # Send back the id that we have assigned to this particular user
    {:ok, %{ id: socket.assigns.user_id }, socket}
  end

  def handle_in("message", %{"to" => to, "body" => body}, socket) do
    socket.endpoint.broadcast("user:" <> to, "message", %{body: body, to: to})
    {:noreply, socket}
  end
end

When I attempt to send the message:

  1. It appears to have been successfully posted (no errors are visible)
  2. A few seconds later, a timeout is acknowledge on the client in the chan_reply_n topic, where n is a number representing the message sequence

My questions are:

  1. Am I doing something wrongly?
  2. How can I debug timeouts?

Thanks very much!!

Showing Posts 1 to 2

kokolegorille

kokolegorille

Just make a user channel, like the room channel, and check on join that a user can only connect to it’s own.

It’s possible to multiplex many channels on a socket, and having a dedicated one per user is quite common.

peoj

peoj OP

Thanks that’s exactly what I have ended up doing. It would have been possible in this case to simplify the client-side code by not needing the additional channel. Is there any way of achieving that?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews