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

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics 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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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 42533 114
New

We're in Beta

About us Mission Statement