<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Wycliff Ogembo</title>
    <description>The latest articles on DEV Community by Wycliff Ogembo (@wycliffogembo).</description>
    <link>https://dev.to/wycliffogembo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3892693%2F69f7633b-f7a1-4ca4-ad5a-e931732f3425.jpg</url>
      <title>DEV Community: Wycliff Ogembo</title>
      <link>https://dev.to/wycliffogembo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wycliffogembo"/>
    <language>en</language>
    <item>
      <title>When your Phoenix socket has no identity at all (and why that was the right call)</title>
      <dc:creator>Wycliff Ogembo</dc:creator>
      <pubDate>Wed, 22 Apr 2026 17:38:59 +0000</pubDate>
      <link>https://dev.to/wycliffogembo/when-your-phoenix-socket-has-no-identity-at-all-and-why-that-was-the-right-call-nm5</link>
      <guid>https://dev.to/wycliffogembo/when-your-phoenix-socket-has-no-identity-at-all-and-why-that-was-the-right-call-nm5</guid>
      <description>&lt;p&gt;Most Phoenix Channel tutorials assume the socket carries an authenticated identity — a user token, a session cookie, something that &lt;code&gt;connect/3&lt;/code&gt; validates. That's the path of least resistance and it works for 95% of apps.&lt;/p&gt;

&lt;p&gt;I ended up writing one where it was actively wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  The setup
&lt;/h3&gt;

&lt;p&gt;I was building a zero-knowledge messaging app — two people share a secret and talk through a web channel, and the entire threat model hinges on the server not knowing who is talking to whom. If the socket had any durable identity, it would become a correlatable identifier. That identifier would exist in logs, in crash dumps, in whatever the BEAM's &lt;code&gt;Process.info&lt;/code&gt; returns during a hot debug session.&lt;/p&gt;

&lt;p&gt;I needed a socket the server literally could not identify.&lt;/p&gt;

&lt;h3&gt;
  
  
  The code
&lt;/h3&gt;

&lt;p&gt;It turns out this is shorter than the normal path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;MyAppWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;AnonSocket&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Phoenix&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Socket&lt;/span&gt;
  &lt;span class="n"&gt;channel&lt;/span&gt; &lt;span class="s2"&gt;"anon_room:*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;MyAppWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;AnonRoomChannel&lt;/span&gt;

  &lt;span class="nv"&gt;@impl&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_connect_info&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nv"&gt;@impl&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_socket&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. &lt;code&gt;connect/3&lt;/code&gt; accepts everyone. &lt;code&gt;id/1&lt;/code&gt; returns &lt;code&gt;nil&lt;/code&gt;, which tells &lt;code&gt;Phoenix.Socket&lt;/code&gt; there is no identifier to use for per-user broadcasts. The socket is equally anonymous to Phoenix and to anyone reading the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where auth actually lives
&lt;/h3&gt;

&lt;p&gt;All access control moves into &lt;code&gt;Channel.join/3&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"anon_room:"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;room_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="p"&gt;%{&lt;/span&gt;&lt;span class="s2"&gt;"access_hash"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;access_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sender_hash"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sender_hash&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;

  &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="ss"&gt;:ok&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;validate_hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;room_hash&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
       &lt;span class="ss"&gt;:ok&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;validate_hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;access_hash&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
       &lt;span class="ss"&gt;:ok&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;validate_hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender_hash&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
       &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="no"&gt;Rooms&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_active_room&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;room_hash&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
       &lt;span class="ss"&gt;:ok&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="no"&gt;Rooms&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;verify_access&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;room_hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;access_hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;room:&lt;/span&gt; &lt;span class="n"&gt;room&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;sender_hash:&lt;/span&gt; &lt;span class="n"&gt;sender_hash&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;%{&lt;/span&gt;&lt;span class="ss"&gt;reason:&lt;/span&gt; &lt;span class="s2"&gt;"unauthorized"&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The three hashes are all SHA-256 hex strings computed client-side from a shared secret. The server verifies them against its database but never sees the secret itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you give up
&lt;/h3&gt;

&lt;p&gt;Phoenix's per-user utilities stop working because there is no "user":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Phoenix.PubSub&lt;/code&gt; keyed by &lt;code&gt;socket.id&lt;/code&gt;&lt;/strong&gt; — doesn't apply; there's nothing to key on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Presence tracking by user ID&lt;/strong&gt; — works only at the topic level (who is in &lt;code&gt;anon_room:&amp;lt;hash&amp;gt;&lt;/code&gt;), not across topics for the same user.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server-side rate limiting by identity&lt;/strong&gt; — you have to fall back to IP-based rate limiting at the endpoint/plug layer, since the channel has no identity to throttle.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What you keep
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The channel itself still works normally — push, broadcast-to-topic, &lt;code&gt;handle_in&lt;/code&gt;, all of it.&lt;/li&gt;
&lt;li&gt;You can still store assigns per-socket (&lt;code&gt;assign(socket, room: room)&lt;/code&gt;). You just can't share identity &lt;em&gt;across&lt;/em&gt; sockets for the same user.&lt;/li&gt;
&lt;li&gt;A smaller attack surface: a socket that never authenticates cannot leak authentication state.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The useful mental model
&lt;/h3&gt;

&lt;p&gt;Normal Phoenix sockets are like a long-lived session: you log in once, then every channel inherits that identity. The sessionless variant is more like a capability-URL system — each channel join carries its own bearer credentials, and the socket is just a pipe.&lt;/p&gt;

&lt;p&gt;For most apps this is unnecessary complication. For apps where the socket &lt;em&gt;mustn't&lt;/em&gt; correlate activity across channels, it's the simpler mental model.&lt;/p&gt;




&lt;p&gt;If you've got a use case where this pattern fits, I'd love to hear about it — I've only seen it come up a couple of times. (The app that drove this design: &lt;a href="https://github.com/stelgano/stelgano" rel="noopener noreferrer"&gt;sTELgano&lt;/a&gt;, AGPL-3.0.)&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>webdev</category>
      <category>security</category>
      <category>webcrypto</category>
    </item>
  </channel>
</rss>
