UsamaHaide0786

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

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement