alex88

alex88

How to send a message to a user without having to join a channel client side

Is there a way to send a message to a specific user without having to join a channel client side? Especially without having to create a channel per user since the client doesn’t even know it’s user id (so no users:#{id} channels)

Most Liked

kokolegorille

kokolegorille

This is best handle with a user:id channel :slight_smile:

The server can send to any specific user, via its own channel like this.

socket.endpoint.broadcast!("user:#{id}", message, payload)

The user does not need to know the id, the server knows it. But for some reason, You don’t want to do this…

mgwidmann

mgwidmann

An alternative also is to have every client join the same channel, i.e. users, and if you broadcast something like MyApp.Endpoint.broadcast("users", "message", %{for_user: 123, data: "here"}). You can then use phoenix’s intercept to determine within the channel if that message is “for you”, assuming you know server side the ID of the user who joined. This would mean your “broadcasts” would only go out to the parties which your intercept function determined it should go out. Be aware that intercept has significant performance penalties and you should heavily consider them before using it.

ryh

ryh

You give them a unique identifier. Generate a UUID, set it in a cookie, and use it to build the topic to join. No authentication and the user doesn’t need to know. You can even generate the UUID client side if you’re okay with the security implications.

You’re not going to be able to send a specific message to a specific user without giving them some kind of unique identifier.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement