UsamaHaide0786
My chat app using liveViews is not updating every user in same room
I have created a chat app using live views in which whenever user joins a chatroom then all the messages of the chat room displayed to him that are saved in database and if some updates or edit any message it works perfect everyone sees the updated message but sometimes it misses some user screens specially if some leave a chat room open for few hours then the that user stops receiving updates and he need to refresh browser even sometimes browser refresh don’t work
Here is my code in my live view for chat
defmodule FchatappWeb.ChatLive do
use FchatappWeb, :live_view
alias Fchatapp.Blog
alias Fchatapp.Blog.Room
alias Fchatapp.Repo
alias Fchatapp.Chats.Message
import Ecto.Query, only: [from: 2]
@imp true
def mount(%{"room" => name}, session, socket) do
room_data = Repo.get_by(Room, title: name)
room_data_id = room_data.id
query =
from m in Message,
where: m.room_id == ^room_data_id
db_messages = Repo.all(query)
topic = "room:" <> name
email =
case session["current_user"] do
nil ->
"Anonymous User"
_ ->
session["current_user"].email
end
anonymous_user =
case email do
"Anonymous User" ->
true
_ ->
false
end
if connected?(socket) do
Process.send_after(self(), %{event: "update", room_id: room_data_id}, 5000)
FchatappWeb.Endpoint.subscribe(topic)
FchatappWeb.Presence.track(self(), topic, email, %{})
end
{:ok,
assign(socket,
room: name,
room_id: room_data.id,
image: room_data.image_thumb_url,
country: room_data.country,
state: room_data.state,
city: room_data.city,
description: room_data.description,
topic: topic,
user: email,
anonymous_user: anonymous_user,
message: "",
image_public_id: "",
image_thumb_url: "",
image_url: "",
image_secure_url: "",
img_context: "",
messages: db_messages,
user_list: [],
temporary_assigns: [messages: []]
)}
end
@imp true
def handle_event(
"submit_message",
%{
"_csrf_token" => _token,
"chat" => %{
"message" => message,
"image_public_id" => image_public_id,
"image_thumb_url" => image_thumb_url,
"image_url" => image_url,
"image_secure_url" => image_secure_url,
"room_id" => room_id,
"img_context" => img_context
}
},
socket
) do
message = %{
uid: UUID.uuid4(),
content: message,
user: socket.assigns.user,
image_public_id: image_public_id,
image_thumb_url: image_thumb_url,
image_url: image_url,
image_secure_url: image_secure_url,
room_id: room_id,
img_context: img_context
}
case image_url do
"" ->
{:ok}
_ ->
resp =
HTTPoison.post(
"http://localhost:4000/api/create-message",
message |> Poison.encode!(),
%{"Content-Type" => "application/json"}
)
end
FchatappWeb.Endpoint.broadcast(socket.assigns.topic, "new-message", message)
{:noreply,
assign(socket,
message: "",
image_public_id: "",
image_thumb_url: "",
image_url: "",
image_secure_url: ""
)}
end
@imp true
def handle_event(
"form_update",
%{
"_csrf_token" => _token,
"chat" => %{
"message" => message,
"image_public_id" => image_public_id,
"image_thumb_url" => image_thumb_url,
"image_url" => image_url,
"image_secure_url" => image_secure_url,
"room_id" => room_id,
"img_context" => img_context
}
},
socket
) do
{:noreply,
assign(socket,
message: message,
image_public_id: image_public_id,
image_thumb_url: image_thumb_url,
image_url: image_url,
image_secure_url: image_secure_url,
room_id: room_id,
img_context: img_context
)}
end
@imp true
def handle_info(%{event: "new-message", payload: message}, socket) do
{:noreply, assign(socket, messages: [message])}
end
def handle_info(%{event: "update", room_id: id}, socket) do
Process.send_after(self(), %{event: "update", room_id: id}, 5000)
query =
from m in Message,
where: m.room_id == ^id
db_messages = Repo.all(query)
{:noreply, assign(socket, messages: db_messages)}
end
def handle_info(%{event: "presence_diff", payload: %{joins: joins, leaves: leaves}}, socket) do
join_messages =
joins
|> Map.keys()
|> Enum.map(fn username ->
%{uid: UUID.uuid4(), content: "#{username} Joined!", user: "System"}
end)
leave_messages =
leaves
|> Map.keys()
|> Enum.map(fn username ->
%{uid: UUID.uuid4(), content: "#{username} Left!", user: "System"}
end)
user_list =
FchatappWeb.Presence.list(socket.assigns.topic)
|> Map.keys()
{:noreply, assign(socket, user_list: user_list)}
end
def display_message(%{uid: id, content: content, user: "System"}) do
~E"""
<p id="<%= id %>"><em><%= content %></em></p>
"""
end
def display_message(
%{
uid: id,
content: content,
user: user,
image_public_id: image_public_id,
image_thumb_url: image_thumb_url,
image_url: image_url,
image_secure_url: image_secure_url
} = params
) do
case image_url do
"" ->
~E"""
<div id="<%= id %>">
<img src="https://media.istockphoto.com/vectors/emoticon-with-sorry-sign-vector-id1266144552?k=20&m=1266144552&s=170667a&w=0&h=m-3MDOgzYDjsynL729p7G8Pn98PNKLu345LnFwkcZlY=" width="100" height="150"></img>
<p><em>Image cant be empty</em></p>
</div>
"""
_ ->
~E"""
<div id="<%= id %>">
<div><img src="<%= image_url%>" width="100" height="150"></img>
<p><strong><%= user%>: </strong><%= content %> </p>
</div>
</div>
"""
end
end
end
Here is template hidden inputs are coming from an api
<section>
<div id="header-text">
<div id="title">
<p >Currently Chatting In <strong><%= @room %></strong></p>
<p ><img src="<%= @image %>" alt="no-image"></p>
<p ><%= @description %></p>
<button><a href="/">Leave Room</a></button>
</div>
</div>
<br>
<div id="main">
<div class="sub-main">
<div id="chat-container">
<div id="messages-container" phx-update="append">
<%= for message <- @messages do%>
<%= display_message(message) %>
<% end %>
</div>
</div>
<%= if @anonymous_user do %>
<p><strong><em>Please login to send messages</em></strong></p>
<% else %>
<div id="form-chat">
<%= f = form_for :chat, "#", id: "chat-form", phx_submit: :submit_message, phx_change: :form_update %>
<%= text_input f, :message, value: @message, placeholder: "Enter your message..." %>
<%= hidden_input f, :image_public_id, value: @image_public_id, id: "image_public_id" %>
<%= hidden_input f, :image_thumb_url, value: @image_thumb_url, id: "image_thumb_url" %>
<%= hidden_input f, :image_url, value: @image_url, id: "image_url" %>
<%= hidden_input f, :image_secure_url, value: @image_secure_url, id: "image_secure_url" %>
<%= hidden_input f, :room_id, value: @room_id, id: "room_id" %>
<%= hidden_input f, :img_context, value: @img_context, id: "img_context" %>
<!-- placeholder for uploaded image -->
<img
id="uploadedimage"
src="<%= @image_thumb_url %>">
</img>
<button
id="upload_widget"
class="cloudinary-button" type="button">
Upload New Image
</button>
</form>
</div>
<% end %>
</div>
<div id="users">
<h3>
Online User
</h3>
<%= for user <- @user_list do%>
<p><%= user %></p>
<% end %>
</div>
</div>
</section>
Thanks in advance
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
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
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance









