Implement direct messages (no chat room) using phoenix channels

Still all nodes would get the message, since there is no a priori information on which node the other user’s channel is. But why is it a problem anyway @j-deng?

That’s because it doesn’t know which specific node a user is on as they could be *even multiple) connected on any at any time.

If you really want it to go to just one node I’d just use a global multi-node process registry and have the channels register to it with an appropriate user-specific ID, then you can just grab that and send a message straight to the process, bypassing everything else. :slight_smile:

2 Likes

I’d rather use a DHT instead of :global, since it would probably create more problems than chatty broadcasts. But that’s just speculation … Before suggesting anything concrete we’d need to know why cluster-wide broadcasts are problematic for @j-deng.

1 Like

Not heard of that library, I’d probably just use :gproc myself though there are definitely better ones that handle split and such better… ^.^;

:man_facepalming:Oh, sorry, I just meant a distributed hash table.

Lol, that only works if the user always ends up on the same node, which for channel sockets is unlikely, especially if different capabilities on nodes. ^.^;

1 Like

Because i want to create a chat app that mostly message is private between 2 users, i want to know is there a way that to avoid cluster-wide broadcasts, it will be better if possible to broadcast directly to target user

as @OvermindDL1 said, a “user” may be connected to any web facing node in the cluster, so for most use cases it makes sense to perform the cluster-wide broadcast. You may have a user with multiple browser tabs load balanced to different nodes, connected from browser + mobile, smartwatch, etc. So unless you have very specific requirements, you likely want to perform the normal broadcast. If you have a lot of non-web facing nodes that you don’t want to receiving the messages, you can start pubsub servers specific to web and avoid this, but we’d need to know more about your requirements and scale. It is very likely you simply broadcast and carry on :slight_smile:

4 Likes

If You want to do peer to peer connection, You could use webrtc and Phoenix as the signalling server. This way, once the connection get established, You would be directly connected :slight_smile:

1 Like