bananaphone
WebSockex start_link outside GenServer start_link fails in keepalive pattern
I am trying to wrap the WebSockex library so that it is initialized asynchronously, and if the websocket source is unavailable or becomes unavailable, the application stays alive and just retries in 60 seconds. To this end I have created a GenServer, and in its init callback I am doing Process.send_after(self(), :websocket_connect, 0) and my handle_info looks like this:
try do
with {:ok, pid} <-
WebSockex.start_link(
url,
MyHandler,
{},
async: false,
handle_initial_conn_failure: true
) do
Logger.info("Success starting websocket.")
...
rescue
e ->
Logger.error("Runtime error establishing websocket connection")
Process.send_after(self(), :websocket_connect, 60_000)
{:noreply, state}
end
If I call WebSockex.start_link inside a GenServer start_link the command works. If I put it in my handle_info callback it fails with:
14:21:38.470 [error] ** (WithClauseError) no with clause matching: {:function_clause, {WebSockex, :call, [%WebSockex.Conn{conn_mod: :gen_tcp, host: (bunch of websocket connection info)
What am I doing wrong? Is it not possible to call this function outside of another start_link? Or is this an issue with WebSockex?
Marked As Solved
bananaphone
Completely by accident I found the problem, I really was using the library wrong. I completely restructured the code and then Dialyzer told me I was doing something wrong (don’t know why it didn’t complain before or why the original error message was so misleading.) The mistake is not relevant to the question so I won’t clutter the thread with it.









