I have coded simple elixir app, which starts websocket client and listens for incoming messages. I was expecting it to run forever. But the app exits as soon as I run it using mix app.start
. Here is the code. What should I change to make it app run forever?
defmodule Example.WsClient do
use WebSockex
def start_link(state) do
IO.puts "Starting WS Client"
WebSockex.start_link("ws://example.com/ws", __MODULE__, state)
end
def handle_frame({type, msg}, state) do
IO.puts "Received Message - Type: #{inspect type} -- Message: #{inspect msg}"
{:ok, state}
end
def handle_cast({:send, {type, msg} = frame}, state) do
IO.puts "Sending #{type} frame with payload: #{msg}"
{:reply, frame, state}
end
end
defmodule Example.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
# Starts a worker by calling: Example.Worker.start_link(arg)
{Example.WsClient, []}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Example.Supervisor]
Supervisor.start_link(children, opts)
end
end