mythicalprogrammer
Stuck on "Build it with phoenix" 12th video: The other user aren't seeing the broadcast
I’m stuck on broadcasting. The other user aren’t seeing the broadcast. I don’t think the users are subscribed correctly. I have no idea how to debug for subscribe
defmodule ChatWeb.ChatLive do
use ChatWeb, :live_view
def mount(%{"room_id" => room_id}, _session, socket) do
topic = "room:#{room_id}"
if connected?(socket) do
ChatWeb.Endpoint.subscribe(topic)
end
user = MnemonicSlugs.generate_slug(2)
form = to_form(%{"text" => ""}, as: :message)
messages = [Chat.Messages.insert_message("#{user} joined the chat", user: "system")]
{:ok, assign(socket, room: room_id, user: user, topic: topic, form: form, messages: messages), temporary_assigns: [messages: []]}
end
def render(assigns) do
~H"""
<h1 class="text-l">
You are chatting in the <code class="bg-slate-200 p-2"><%= @room %></code>
room as <code class="bg-slate-200 p-2"><%= @user %></code>
</h1>
<div class="mt-4 border-2 p-4" id="message-list" phx-update="append">
<div :for={message <- @messages} id={"message-#{message.id}"}>
<%= message.text %>
</div>
</div>
<div class="mt-4 border-2 p-4">
<div>Chat message</div>
</div>
<.simple_form for={@form} phx-submit="save_message">
<.input field={@form[:text]} placeholder="Enter chat message" />
</.simple_form>
"""
end
def handle_event("save_message", %{"message" => %{"text" => text}}, socket) do
message = Chat.Messages.insert_message(text, socket.assigns.user)
IO.inspect(socket.assigns.topic, label: "broadcast_topic")
ChatWeb.Endpoint.broadcast(socket.assigns.topic, "new-message", message)
{:noreply, assign(socket, messages: [message])}
end
def handle_info(%{event: "new-message", payload: message} = msg, socket) do
IO.inspect(msg, label: "handle_info")
{:noreply, assign(socket, message: [message])}
end
end
I’m trying to debug this and I can see it’s broadcasting.
How do I see if the user is subscribing?
I believe I’m connecting right here:
Rebuilding...
Done in 22ms.
[info] GET /biwp
[debug] Processing with ChatWeb.ChatLive.nil/2
Parameters: %{"room_id" => "biwp"}
Pipelines: [:browser]
[info] Sent 200 in 879µs
[info] GET /biwp
[debug] Processing with ChatWeb.ChatLive.nil/2
Parameters: %{"room_id" => "biwp"}
Pipelines: [:browser]
[info] Sent 200 in 956µs
[info] CONNECTED TO Phoenix.LiveView.Socket in 28µs
Transport: :websocket
Serializer: Phoenix.Socket.V2.JSONSerializer
Parameters: %{"_csrf_token" => "GQJGZ3kxfholD1sjCQVCNlNbJAcoQh0no52--iGKRNlpqm6lk6EAL4YU", "_live_referer" => "undefined", "_mount_attempts" => "0", "_mounts" => "0", "_track_static" => %{"0" => "http://localhost:4000/assets/app.css", "1" => "http://localhost:4000/assets/app.js"}, "vsn" => "2.0.0"}
[debug] MOUNT ChatWeb.ChatLive
Parameters: %{"room_id" => "biwp"}
Session: %{"_csrf_token" => "v7tJTX9QwA7SxhtZ8maFdvDr"}
[debug] Replied in 320µs
[info] CONNECTED TO Phoenix.LiveView.Socket in 18µs
Transport: :websocket
Serializer: Phoenix.Socket.V2.JSONSerializer
Parameters: %{"_csrf_token" => "LlssBWAVdzwfOF4WIVBBIEsHKxEXQSU_XlXO4MNmhyiEY85zsjJWs7aM", "_live_referer" => "undefined", "_mount_attempts" => "0", "_mounts" => "0", "_track_static" => %{"0" => "http://localhost:4000/assets/app.css", "1" => "http://localhost:4000/assets/app.js"}, "vsn" => "2.0.0"}
[debug] MOUNT ChatWeb.ChatLive
Parameters: %{"room_id" => "biwp"}
Session: %{"_csrf_token" => "v7tJTX9QwA7SxhtZ8maFdvDr"}
[debug] Replied in 429µs
And I can see it’s broadcasting:
[debug] MOUNT ChatWeb.ChatLive
Parameters: %{"room_id" => "biwp"}
Session: %{"_csrf_token" => "v7tJTX9QwA7SxhtZ8maFdvDr"}
[debug] Replied in 429µs
[debug] HANDLE EVENT "save_message" in ChatWeb.ChatLive
Parameters: %{"message" => %{"text" => "hi"}}
broadcast_topic: "room:biwp"
[debug] Replied in 577µs
handle_info: %Phoenix.Socket.Broadcast{
topic: "room:biwp",
event: "new-message",
payload: %Chat.Message{
id: "3113670e-61eb-4c0f-99fc-6571af4896db",
text: "hi",
user: "perform-tina"
}
}
handle_info: %Phoenix.Socket.Broadcast{
topic: "room:biwp",
event: "new-message",
payload: %Chat.Message{
id: "3113670e-61eb-4c0f-99fc-6571af4896db",
text: "hi",
user: "perform-tina"
}
}
So it seems like it’s broadcasting but I think the user are subscribed. I have to two tabs opened to the same room, biwp.
I’d like to figure out how to debug and see if the users are subscribed to topic room:biwp.
My phoenix version and live view according to mix.exs is 1.7.14 for phoenix and liveview is 1.0.0-rc.1
Thank you
Marked As Solved
mythicalprogrammer
There was a typo… message: [message] should be messages: [message].
2
Popular in Questions
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
can someone please explain to me how Enum.reduce works with maps
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
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
Got a question about when to concat vs. prepending items to list then reversing to achieve appending.
So i know lists boil down to [1 | ...
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
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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










