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
This is best handle with a user:id channel ![]()
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









