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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Sorry to necro an old thread here, but <a class="mention" href="/u/exadra37" rel="nofollow">@Exadra37</a> did you ever reach a good alternative solution for this? I tried to do a similar approach to your rewrite-as-an-x-header-via-plug approach and came up with the same ineffective result.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="268676" 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/how-to-access-http-headers-in-phoenix-socket/32186/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-268676" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="268676"
                     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 #31"></div>
  </section>
</div>
    <div class="postbit" id="269045" data-post-id="269045">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I had to resort to use the <code>x-header</code> approach enforced by Phoenix, because the framework it’s inflexible in this regard.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="269045" 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/how-to-access-http-headers-in-phoenix-socket/32186/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-269045" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="269045"
                     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 #32"></div>
  </section>
</div>
    <div class="postbit" id="302273" data-post-id="302273">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The reason why this is not allowed is due to poor browser security. WebSockets are cross-domain, which means that, if you authenticate into a WebSocket with cookie/auth headers, an attacker on a completely different website can use the same credentials to open up and control a WebSocket with your credentials on any website.</p>
<p>Even if you plan to use WebSockets outside of the browser, in my humble opinion, just use the x-headers, as otherwise it is not worth introducing such a big hole in your app. If you want to risk it anyway, latest Plug (v1.14+) and Phoenix (v1.7+) allows you to upgrade any connection to a WebSocket, so that could be used instead. Here is an example: <a href="https://github.com/elixir-plug/plug#hello-world-websockets" class="inline-onebox" rel="nofollow">GitHub - elixir-plug/plug: Compose web applications with functions · GitHub</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="302273" 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/how-to-access-http-headers-in-phoenix-socket/32186/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-302273" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="302273"
                     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 #33"></div>
  </section>
</div>
    <div class="postbit" id="309269" data-post-id="309269">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="rjk" data-post="26" data-topic="32186">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rjk/48/1293_2.png" class="avatar"> rjk:</div>
<blockquote>
<p>Another completely different idea (also seen in the wild) is letting clients connect unauthenticated to your socket and let them authenticate with a separate message as the first action after the connection is established, otherwise you just disconnect them.</p>
</blockquote>
</aside>
<p>About the part “<em>otherwise you just disconnect them</em>”, do you have any ideas on how to implement it? I’m assuming you meant “disconnect if the user doesn’t authenticate in X seconds”.</p>
<p><strong>What I have tried</strong></p>
<p>In the user socket’s <code>connect/3</code></p>
<ol>
<li>Schedule an <code>:auth_timeout</code> message using <code>Process.send_after/3</code> to <code>self()</code></li>
<li>Save the timer reference into the socket assigns</li>
<li>Handle the <code>:auth_timeout</code> and disconnects the socket.</li>
</ol>
<p>In the user channel’s <code>join/3</code></p>
<ol>
<li>Cancel the timer once the user authenticates.</li>
</ol>
<p>However, <code>Phoenix.Socket</code> brings its own <code>handle_info(message, state)</code> that matches everything. So instead of sending my custom <code>:auth_timeout</code>, I tried sending a <code>%Broadcast{event: "disconnect"}</code> that I see in the source code. However, it looks the process that runs the <code>connect/3</code> is not the same that would be listening for these messages, so nothing happens.</p>
<p>Do you know which process would it be?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309269" 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/how-to-access-http-headers-in-phoenix-socket/32186/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-309269" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309269"
                     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 #34"></div>
  </section>
</div>
    <div class="postbit" id="309554" data-post-id="309554">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="rogerweb" data-post="35" data-topic="32186">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rogerweb/48/22010_2.png" class="avatar"> rogerweb:</div>
<blockquote>
<p>Do you know which process would it be?</p>
</blockquote>
</aside>
<p>I’ve figured it out: it’s the Cowboy’s process <code>:cowboy_clear.connection_process/4</code>. You can get its PID from the socket’s <code>connect/3</code> like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{:links, [pid]} = Process.info(self(), :links)
</code></pre>
<p>Then you create a timer to disconnect the socket using something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">timer_ref = Process.send_after(pid, %Broadcast{event: "disconnect"}, 3000)
</code></pre>
<p>Save the <code>timer_ref</code> in the socket’s assigns and cancel it later in the Channel’s <code>join</code>, like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Process.cancel_timer(socket.assigns.auth_timer)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309554" data-batch-url="/posts/batch_likers">
                        2
                      </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/how-to-access-http-headers-in-phoenix-socket/32186/36">Post #35</a>
	                </div>
	            </div>
              <div id="likers-container-309554" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309554"
                     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>