<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="217803" data-post-id="217803">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="anuaralfetahe" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/anuaralfetahe/120/29144_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  anuaralfetahe
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Stumbled on the same problems.<br>
So basically I created a GenServer which is wrapper for the WebSockex process.<br>
Starting the GenServer will return the pid no matter if the WebSockex fails on the start_link or not. You actualy start the websocket connection in the handle_continue/2 func asynchrously.<br>
Then you need to specify <code>handle_initial_conn_failure: true</code> option on the WebSockex start_link func so you can handle the error in the handle_disconnect/2 func. In the handle_disconnect/2 func you can return the tuple <code>{:reconnect, state}</code> for automatic reconnecting. I have added some process sleep there aswell. Here’s the code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">      def start_link(args) do
        GenServer.start_link(__MODULE__, args)
      end

      @impl GenServer
      def init(state) do
        {:ok, state, {:continue, :start_ws}}
      end

      @impl GenServer
      def handle_continue(:start_ws, state) do
        {:ok, pid} = WebSockex.start_link(url, __MODULE__, [], [handle_initial_conn_failure: true])

        subscribe(pid, state)

        {:noreply, state}
      end

      @impl WebSockex
      def handle_disconnect(_conn, state) do
        IO.puts("Disconnected!")

        # Wait before trying to reconnect.
        Process.sleep(5000)

        {:reconnect, state}
      end
</code></pre>
<p>Theres also an option called <code>async</code> on the WebSocex library but this does not seem to work with the sending frame or I am missing something.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="217803" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/websockex-an-elixir-websocket-client/4733/52">Post #51</a>
	                </div>
	            </div>
              <div id="likers-container-217803" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="217803"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #51"></div>
  </section>
</div>
    <div class="postbit" id="366271" data-post-id="366271">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="marschro" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/marschro/120/23466_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  marschro
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi all,</p>
<p>I try to understand something.</p>
<p>I have a iot device (shelly switch) that I am connected to via stateful websockex.</p>
<p>When I just disconnect the shelly from the power grid, so that it immediately stops and the socket connection stops, then according to the docs terminate is invoked on our side (the side that created the socket connection)</p>
<p>I have those two callbacks:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"> def terminate({:remote, :closed} = reason, state) do
    Logger.error("[ ShellySocketConnector ] - Terminate: #{inspect(reason)}")
    {:ok, state}
  end

  def terminate(reason, state) do
    Logger.error("[ ShellySocketConnector ] - Terminate: #{inspect(reason)}")
    {:ok, state}
  end
</code></pre>
<p>… but I never get a log when terminating the socket connection by pulling the plug.</p>
<p>So is this expected behavior or am I doing something wrong. I would be happy to find a way, when a client disconnects or more in my case, terminates the socket connection.</p>
<p>Any hint appreciated.</p>
<p>cheers,<br>
Martin</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="366271" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/websockex-an-elixir-websocket-client/4733/53">Post #52</a>
	                </div>
	            </div>
              <div id="likers-container-366271" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="366271"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #52"></div>
  </section>
</div>
    <div class="postbit" id="366272" data-post-id="366272">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="onebox allowlistedgeneric" data-onebox-src="https://elixir.hexdocs.pm/1.12/GenServer.html">
  <header class="source">

      <a href="https://elixir.hexdocs.pm/1.12/GenServer.html" target="_blank" rel="noopener nofollow">elixir.hexdocs.pm</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://elixir.hexdocs.pm/1.12/GenServer.html" target="_blank" rel="noopener nofollow">GenServer — Elixir v1.12.3</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Check the conditions required for terminate to be called.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="366272" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/websockex-an-elixir-websocket-client/4733/54">Post #53</a>
	                </div>
	            </div>
              <div id="likers-container-366272" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="366272"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>