Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

User Interaction

usr_intraction.livemd

User Interaction

Mix.install([
  {:phoenix_gen_socket_client, "~> 4.0"},
  {:websocket_client, "~> 1.4"},
  {:jason, "~> 1.3"},
  {:slipstream, "~> 1.0"}
])
:ok

Socket creation

defmodule BeluSocket do
  use Slipstream,
    restart: :temporary

  require Logger

  @topic "userfeed:def26dfa-8064-4096-a95e-0caa4c7444b5"

  def start_link(args) do
    Slipstream.start_link(__MODULE__, args, name: __MODULE__)
  end

  @impl Slipstream
  def init(config) do
    case connect(config) do
      {:ok, socket} ->
        timer = :timer.send_interval(1000, self(), :request_metrics)
        {:ok, assign(socket, :ping_timer, timer)}

      {:error, reason} ->
        Logger.error(
          "Could not start #{__MODULE__} because of " <>
            "validation failure: #{inspect(reason)}"
        )

        :ignore
    end
  end

  @impl Slipstream
  def handle_connect(socket) do
    {:ok, join(socket, @topic)}
  end

  @impl Slipstream
  def handle_join(@topic, _join_response, socket) do
    # an asynchronous push with no reply:
    push(socket, @topic, "ping", %{"hello" => "there"})

    {:ok, socket}
  end

  @impl Slipstream
  def handle_continue(:start_ping, socket) do
    timer = :timer.send_interval(1000, self(), :request_metrics)

    {:noreply, assign(socket, :ping_timer, timer)}
  end

  @impl Slipstream
  def handle_message(@topic, "loc_reverie", message, socket) do
    Logger.info(
      # <>
      "Orbs Remembered in Location"
    )

    IO.inspect(message)

    {:ok, socket}
  end

  @impl Slipstream
  def handle_message(@topic, event, message, socket) do
    Logger.error(
      "Was not expecting a push from the server. Heard: " <>
        inspect({@topic, event, message})
    )

    {:ok, socket}
  end

  @impl Slipstream
  def handle_disconnect(_reason, socket) do
    :timer.cancel(socket.assigns.ping_timer)

    {:stop, :normal, socket}
  end

  @impl Slipstream
  def handle_info(:stop, socket) do
    IO.puts("sayonara mai gud ser")
    {:stop, :normal, socket}
  end

  @impl Slipstream
  def handle_info(:request_metrics, socket), do: {:noreply, socket}

  @impl Slipstream
  def terminate(reason, socket) do
    Logger.debug("shutting down: " <> inspect(reason))

    disconnect(socket)
  end
end
{:module, BeluSocket, <<70, 79, 82, 49, 0, 0, 29, ...>>, {:terminate, 2}}
token =
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjQzNDU5MTEsImlhdCI6MTY2NDM0NDY5OSwiaXNzIjoiUHJpbmNldG9uIiwicm9sZSI6InBsZWIiLCJzdWIiOiJTY3JhdGNoQmFjIiwidXNlcl9pZCI6ImRlZjI2ZGZhLTgwNjQtNDA5Ni1hOTVlLTBjYWE0Yzc0NDRiNSJ9.utw-78_NAvEwcO5eRnuQJLplUfqGF10Eck48xFt2-0s"

domain = "ws://localhost:4000"

config = [
  uri: domain <> "/socket/websocket?token=" <> token,
  reconnect_after_msec: [200, 500, 1_000, 2_000]
]

# {:ok, pid} = BeluSocket.start_link(config)
{:error, {:already_started, pid}} = BeluSocket.start_link(config)
{:error, {:already_started, #PID<0.286.0>}}
Process.alive?(pid)
true

Section