Fregate
Run code after user joins Channel
I’m using Phoenix Channels in my app.
Say, I want to send user “Hello” message after he joins the channel.
Consider this pseudocode:
def join("user:" <> user_id, params, socket) do
UserContext.send_hello(user_id) # broadcasts to "user:<user_id>" topic
{:ok, socket}
end
It doesn’t work. The problem, I think, is this broadcasts “hello” message to the topic before user “finished” joining the channel. I can see in server logs that first “hello” function runs, and only then I get [info] JOINED user:a7cf7525-68a6-4b51-bc64-19fdef7f58b7 in 12ms
What’s the best way to run code after user is “joined”?
Marked As Solved
kokolegorille
def join("user:" <> user_id, params, socket) do
send(self(), {:after_join, user_id})
{:ok, socket}
end
def handle_info({:after_join, user_id}, socket) do
UserContext.send_hello(user_id) # broadcasts to "user:<user_id>" topic
{:noreply, socket}
end
6
Popular in Questions
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 record...
New
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.
Dep...
New
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
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
Other popular topics
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
I would like to know what is the best IDE for elixir development?
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
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
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New







