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} <-
               async: false,
               handle_initial_conn_failure: true
             ) do
        Logger.info("Success starting websocket.")


      e ->
        Logger.error("Runtime error establishing websocket connection")
        Process.send_after(self(), :websocket_connect, 60_000)
        {:noreply, state}

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?

How is the code you’ve posted connected to the error message? It’s tricky to say for sure without a stack trace, but I don’t see any with statements inside the implementation of WebSockex.start_link so it doesn’t seem directly related.

The error is also concerning: the thing it’s trying to match looks like an exception payload ({:function_clause, {WebSockex, :call, [%WebSockex.Conn{) caused by something trying to call WebSockex.call(conn, ...) which isn’t a function defined in WebSockex.

Yeah from what I can tell it looks like WebSockex is trying to do a callback on the module its start_link is called from, although when I look through the code on GitHub I don’t see it doing that, I am new to Elixir so I’m not so great at reading it yet though. (EDIT: it is not doing this.)

Mainly I was hoping someone on here could tell me if, categorically, calling start_link on a process thingy in a handle_info is not allowed or this is probably some kind of WebSockex problem. Now I am leaning toward WebSockex problem. The docs for WebSockex say it is implemented as a “special process.”

Basically I am trying to write wrapper code that asynchronously starts websocket connections and can just retry on a delay if the remote connection isn’t available yet, as a way to make my application more resilient, as it should continue to run even if it can’t establish a websocket connection yet. This thread and comment was the impetus: How to make a Supervisor **never** die? - #23 by brucepomeroy

WebSockex doesn’t seem amenable to this since I can’t start it outside start_link so I might have to just get better at Elixir and fork the project.

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.