max-vc

max-vc

Hi All,

I am building a instant messaging app with Phoenix backend.
I use unique user name as the channel name,so user can send msg to each other base on name.
Now I have two nodes running, and they connect each other.

The question is how can I know the message can not reach to other node, for example the target node crash.
and shall I check the user, connect to which node, and check if the node is online, before boradcast or send the message?

Thanks.

Showing Posts 1 to 4

hubertlepicki

hubertlepicki

It is very little info about what you are doing in terms of how your infrastructure and code is set up, but sending back acknowledged message generally seems like the way to go…

max-vc

max-vc OP

Thanks for the suggestion.
yes, I shall user ACK flag to decide whether resend message when user connect back.

here is the code I broadcast user’s message,
the question is shall I check the user or user’node is online , before broadcast the message?

def handle_in("new_message", payload, socket) do
    %{"to" => to_username, "message" =>  conversation} = payload
    from_username = socket.assigns.username
    message = %{
      from:from_username,
      message: conversation
    }
    # shall I find channel dm:`to_username`'s node whether is online,
    # if not, store the message to database,and send again when user's node online again
    socket.endpoint.broadcast!("dm:#{to_username}", "new_message", message)
 end
hubertlepicki

hubertlepicki

I think the main problem here is that you are using Phoenix.PubSub (endpoint.broadcast), which is precisely what it says it is: a pub-sub mechanism. The pub-sub mechanisms generally are fire and forget, and Phoenix does it precisely this way. So, if you want to listen to some ack message it is up to you for implementation and it won’t be super easy, as you have to handle timeouts, out-of-order messages etc.

What I would do instead is to maybe register process globally within a cluster and then perform a normal GenServer.call. GenServer already sets up monitor on the destination process, and will error if the pid you are trying to send message to crashes, it also waits for the response blocking so after your GenServer.call finishes, you will be sure the destination process did in fact receive message and didn’t crash processing it.

There is a number of ways to perform gluster-wide process registration, and maybe you’d need grouping them too. There’s pg2 and Horde.Registry but also :global and others. I would have a look at Horde as this is what I am using and works pretty much flawlessly in my, relatively simple, scenarios.

max-vc

max-vc OP

Thank you so much.
I am not aware Elixir has this concept: process registration. since your last reply, I am looking at Phoenix Tracker, try to find a way to register the user-process info when join the channel.
seems the Horde.Registry is the answer.
Sir, last question, as the IM app may has millions clients connected, shall I worry the performance when use Horde.Registery?

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews