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

Phoenix Socket Realm

usr_channelling.livemd

Phoenix Socket Realm

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

Root

Location

defmodule FloatingDownSockets do
  use Slipstream,
    restart: :temporary

  require Logger

  @channel "archetype:loc:b2f8be78-cf09-4ddb-aa67-1856425c75cf"

  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} ->
        {:ok, socket}

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

        :ignore
    end
  end

  def handle_connect(socket) do
    {:ok, join(socket, @channel)}
  end

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

    {:ok, socket}
  end

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

    IO.inspect(message)

    {:ok, socket}
  end

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

    {:ok, socket}
  end

  @impl Slipstream
  def handle_disconnect(reason, socket) do
    IO.inspect(reason)

    {:stop, :normal, socket}
  end

  @impl Slipstream
  def handle_call({:geocalibrate, message}, _from, socket) do
    IO.puts("message recieved geocalibrate: #{inspect(message)}")
    push(socket, @channel, "location_update", message)
    {:reply, {:ok, :hao}, socket}
  end

  @impl Slipstream
  def handle_call({:say, goodbyein}, _from, socket) do
    IO.puts("server going bye bye in T-#{inspect(goodbyein)}s")
    :timer.send_interval(:timer.seconds(goodbyein), self(), :stop)
    {:reply, {:ok, :saygoodbye}, socket}
  end

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

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

    disconnect(socket)
  end
end
# @token "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiUjNWc2pxOUNBUmZwcndPSnBzNDRrYlZjekZRMiIsInJvbGUiOiJwbGViIiwidGVycml0b3J5Ijp7ImxpdmUiOnsicmFkaXVzIjo4LCJoYXNoIjoiODg2NTIwY2I1YmZmZmZmIn19LCJ1c2VybmFtZSI6IlJ5YW4iLCJpYXQiOjE2NTY2ODE2NjAsImV4cCI6MTY2ODY4MTY2MCwiaXNzIjoiUHJpbmNldG9uIiwic3ViIjoiU2NyYXRjaEJhYyJ9.gVJ0yrF8df9NfT6KGh4T5TkFPtXB_zM_VqOKydmRuOc"
token =
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmeXJfaWQiOiJCWDFCeE05S2t3ZEZteTJKNEZOTmtGeGNscFMyIiwidGVycml0b3J5Ijp7ImhvbWUiOnsiaGFzaCI6NjI4MDIxMjk3NjY2MjI0MTI3LCJyYWRpdXMiOjE2NjU4MzA5NTV9LCJ3b3JrIjp7Imhhc2giOjYyNzk4NjQ0Njk1MjM0OTY5NSwicmFkaXVzIjoxNjY1ODMwOTU1fX0sInVzZXJfaWQiOiJiMmY4YmU3OC1jZjA5LTRkZGItYWE2Ny0xODU2NDI1Yzc1Y2YiLCJ1c2VybmFtZSI6InNva2thIiwiZXhwIjoxNjY2MTYyMjM0LCJpYXQiOjE2NjYxNjEwMjIsImlzcyI6IlByaW5jZXRvbiIsInJvbGUiOiJwbGViIiwic3ViIjoiU2NyYXRjaEJhYyJ9.pf8sX3H1mc-k0UihJF93iQndn_oKxoM0f2-gGXdlJ7I"

# @domain "wss://nyx.scrb.ac"
domain = "ws://localhost:4000"

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

{:ok, pid} = FloatingDownSockets.start_link(config)
message = %{"name" => "home", "geohash" => 628_021_297_666_224_127}
GenServer.call(pid, {:geocalibrate, message})
# saygoodbye
# GenServer.call(pid, {:say, 1})
# message = %{"destination" => "albert", "destination_archetype" => "USR", "message" => "hi satrio"}
# Phoenix.Channels.GenSocketClient.call(pid, {:message, message})
message = %{"name" => "home", "geohash" => 623_276_216_929_026_047}
# Phoenix.Channels.GenSocketClient.call(pid, {:geocalibrate, message})
# {:ok, ref} = push(socket, "archetype:loc:R3Vsjq9CARfprwOJps44kbVczFQ2", :geocalibrate, message)

Chat Socket

defmodule ChattingDownSockets do
  use Slipstream,
    restart: :temporary

  require Logger

  @channel "archetype:usr:6d7b0e7c-a633-4a8a-8d85-b8a493109bd4"

  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} ->
        {:ok, socket}

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

        :ignore
    end
  end

  def handle_connect(socket) do
    {:ok, join(socket, @channel)}
  end

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

    {:ok, socket}
  end

  @impl Slipstream
  def handle_message(@channel, "shout", message, socket) do
    Logger.info(
      # <>
      "Message shouted"
    )

    IO.inspect(message)

    {:ok, socket}
  end

  def handle_message(@channel, "reverie", message, socket) do
    Logger.info(
      # <>
      "Messages Remembered"
    )

    IO.inspect(message)

    {:ok, socket}
  end

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

    {:ok, socket}
  end

  @impl Slipstream
  def handle_disconnect(reason, socket) do
    IO.inspect(reason)

    {:stop, :normal, socket}
  end

  @impl Slipstream
  def handle_call({:shout, message}, _from, socket) do
    IO.puts("message shouted: #{inspect(message)}")
    push(socket, @channel, "shout", message)
    {:reply, {:ok, :hao}, socket}
  end

  @impl Slipstream
  def handle_call({:say, goodbyein}, _from, socket) do
    IO.puts("server going bye bye in T-#{inspect(goodbyein)}s")
    :timer.send_interval(:timer.seconds(goodbyein), self(), :stop)
    {:reply, {:ok, :saygoodbye}, socket}
  end

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

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

    disconnect(socket)
  end
end
token =
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmeXJfaWQiOiJkMXdreVFkNEJpUzlSenZwUURSTDgybGY3WnIxIiwidGVycml0b3J5Ijp7ImhvbWUiOnsiaGFzaCI6NjI4MDIxMjk3NjY2MjI0MTI3LCJyYWRpdXMiOjE2ODQ5OTIzNzN9LCJsaXZlIjp7Imhhc2giOjYyNzc3OTQxMjUyMDE3NzY2MywicmFkaXVzIjoxNjg0OTkyMzczfX0sInVzZXJfaWQiOiIzZDUzNDU0Ny1lZWQyLTRhYjItYWNjOC1lYmYwYWUwM2FhYTUiLCJ1c2VybmFtZSI6ImRlZXh1cGVyeSIsImV4cCI6MTY4NjY2MDk5NSwiaWF0IjoxNjg2NjU5NzgzLCJpc3MiOiJQcmluY2V0b24iLCJyb2xlIjoicGxlYiIsInN1YiI6IlNjcmF0Y2hCYWMifQ.8mkg3m7bX83PymJ-kGQtqF91wJxXzNpk8KUIFjGdlaw"

# @domain "wss://nyx.scrb.ac"
domain = "ws://localhost:4000"

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

{:ok, pid} = ChattingDownSockets.start_link(config)
message = %{
  source: "b2f8be78-cf09-4ddb-aa67-1856425c75cf",
  destination: "b2f8be78-cf09-4ddb-aa67-1856425c75cf",
  destination_archetype: "USR",
  subject_archetype: "ORB",
  subject: "LMAO",
  message: "hello"
}

GenServer.call(pid, {:shout, message})

Cleaning up

Phoenix.Channels.GenSocketClient.call(pid, {:say, 1})