idi527
How to put something in a socket's assigns from another process
I would like to implement a banned feature in a chat and I think the easiest way would be to somehow put %{banned_in: ["chat:1", "chat:4", ...]} in the banned socket’s assigns.
Is there an easy way to do this?
Marked As Solved
OvermindDL1
That is because you are broadcasting to the id topic, which is good for pretty much only killing the channel entirely and all topics built on it, this is not what you want.
Instead you need to put something like MyServer.Endpoint.subscribe("UserChannelBlah:#{socket.assigns.user_id}") in your UserChannel module, say in the join function as an example, and you need to listen for that message as your handle_info is already doing fine (though matching on the message event is useful).
Also Liked
OvermindDL1
Yep yep, try to make sure the topic names would be unique of course, so you’d want to prepend something like "UserChannel:"<>user_id or so just to make sure. ![]()
A direct subscribe like that links the current existing process by storing its pid in the global table, so no real overhead for that. Broadcasting just does a lookup in the global table for the PID(s) and then just sends a message like normal, so trivial there either. In other words, this already is the optimized path short of really low-level stuff that you honestly should not ever do. ![]()
OvermindDL1
Yep, send a message from that other process to the process that you want to change the asisgn in and let it change it itself. ![]()
That is the Actor way to change any data in a process from elsewhere. ^.^
OvermindDL1
Register it somewhere is the traditional way, however it sounds like you are using Phoenix channels, in which case it has a system for this for you already! ![]()
Look in the Channels section (that has no hotlink sadly) in Phoenix.Endpoint — Phoenix v1.8.8 for details on the calls, but basically:
Have each user socket that you want to listen (the one where you want to add the banned part) register itself to a unique user topic, something like MyEndpoint.subscribe(user_id) then you can just MyEndpoint.broadcast(user_id, :add_to_ban_list, ["blah", "blorp"]) or something like that. ![]()
Last Post!
OvermindDL1
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









