idi527

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

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

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. :slight_smile:

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. :slight_smile:

OvermindDL1

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. :slight_smile:

That is the Actor way to change any data in a process from elsewhere. ^.^

OvermindDL1

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! :slight_smile:

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. :slight_smile:

Last Post!

OvermindDL1

OvermindDL1

Looks like it yeah, that is how I’ve used it at least and it works. :slight_smile:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement