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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="smithaitufe" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  smithaitufe
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I simply wish the best for Elixir and Phoenix.</p>
<p><a class="mention" href="/u/josevalim" rel="nofollow">@josevalim</a> I suggested in a separate thread for the Elixir and Phoenix team to create samples that can help people like us to grasp these concepts. That is one thing that helps languages/frameworks like aspnetcore, angular2 etc.</p>
<p>If there are samples backing all the explanations you and <a class="mention" href="/u/chrismccord" rel="nofollow">@chrismccord</a> have given, you will have to talk less.</p>
<p>Thanks for the upcoming RC. Any idea the release date?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="29280" data-batch-url="/posts/batch_likers">
                        5
                      </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-determine-contexts-with-phoenix-1-3/4367/53">Post #52</a>
	                </div>
	            </div>
              <div id="likers-container-29280" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29280"
                     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="30389" data-post-id="30389">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi, I’m new just coming from Rails, but I saw the video talking about Phoenix 1.3 and was amazed about the new contexts concept. I am eager to build a little application with this.<br>
One thing related I am confused about is where I should put the intermediate table. For instance, here I want to create a chat application, I have a <code>User</code> and <code>Room</code>. It should be a N-M association.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">app
 |--account/
 |   |--user.ex
 |--account.ex
 |
 |--services/
 |   |--room.ex
 |-- services.ex
</code></pre>
<p>And now I’m wondering where I should put my <code>room_users.ex</code>. I want to separate <code>User</code> and <code>Room</code> because User should be used in many other places. What should I do?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30389" 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-determine-contexts-with-phoenix-1-3/4367/54">Post #53</a>
	                </div>
	            </div>
              <div id="likers-container-30389" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30389"
                     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 #53"></div>
  </section>
</div>
    <div class="postbit" id="30404" data-post-id="30404">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Well, depends on how you want to approach it.</p>
<p>The first approach I would see is to have tables namespaced by context with no relations cross-contexts schemas. So you would have two different tables for users  - one in the services context and another in the account context. The tricky part is to coordinate changes between then, which you could do using transactions - again, passing through all contexts that have the user schema for example if you want to change the user. The other way is to create a small <code>GenServer</code> module, where you would do a simple <code>PubSub</code> registration/listening and, everytime one of your users tables are changed it should broadcast a message with relevant information that will be captured by the other <code>PubSub</code> <code>GenServer</code>s, so it can be propagated into their own versions of users.</p>
<p>The first approach albeit simple, when you need to scale into microservices your monolith will force you towards the second approach and you’ll have lots of pain because you coupled your cross-contexts relations in the same app. The second approach is more decoupled and, as I stated before, can be moved towards its own service, separated database whatsoever without you caring much of what’s going on.</p>
<p>It’s either a mixture of personal taste and what you’ll need. Do you think you’ll need to scale to millions of users? If yes, then, I would go second approach. If not, well, the first one is cheap and does the job as well.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30404" 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-determine-contexts-with-phoenix-1-3/4367/55">Post #54</a>
	                </div>
	            </div>
              <div id="likers-container-30404" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30404"
                     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 #54"></div>
  </section>
</div>
    <div class="postbit" id="30427" data-post-id="30427">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think your approach is pretty well tied to the Rails way of thinking here. What you’re trending towards is tightly coupled contexts, which is worse than just building a monolith.</p>
<p>For example, why are you storing them in the database? A user being in a room is inherently session-based—this is where you should maybe reach for a GenServer instead of writing to the database. If you want to store a list of rooms the user should be automatically logged into when returning, maybe consider a context for user preferences.  Then a layer higher up can orchestrate between the preferences and the room subscriptions.</p>
<p>Secondly, why does the room need to know anything about the user, like its account information? Would simply telling it which IDs or nicks that are currently in the room suffice for it to do its job?</p>
<p>Try to take a step back and ask why it seems hard when you run into questions like this. Generally I find that trying to puzzle where a particular notion goes is a signal to me that I’ve gotten a boundary wrong.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30427" data-batch-url="/posts/batch_likers">
                        3
                      </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-determine-contexts-with-phoenix-1-3/4367/56">Post #55</a>
	                </div>
	            </div>
              <div id="likers-container-30427" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30427"
                     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 #55"></div>
  </section>
</div>
    <div class="postbit" id="30496" data-post-id="30496">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="smithaitufe" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  smithaitufe
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I really like the second approach. I personally I am struggling to understand the <strong>GenServer</strong> stuff. I don’t know if you can write a sample on how PubSub can be achieved.</p>
<p>I read about <a href="https://keyholesoftware.com/2016/03/21/implementing-a-bounded-context/" rel="noopener nofollow ugc">bounded context</a> yesterday and I was wondering the best way to implement it in Phoenix.</p>
<p>Thanks for your response</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30496" 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-determine-contexts-with-phoenix-1-3/4367/57">Post #56</a>
	                </div>
	            </div>
              <div id="likers-container-30496" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30496"
                     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 #56"></div>
  </section>
</div>
    <div class="postbit" id="30505" data-post-id="30505">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/smithaitufe" rel="nofollow">@smithaitufe</a> I suggest you create a small elixir application and play with <code>Supervisor</code>s, <code>GenServer</code>s, <code>GenStage</code>s, etc… <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30505" 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-determine-contexts-with-phoenix-1-3/4367/58">Post #57</a>
	                </div>
	            </div>
              <div id="likers-container-30505" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30505"
                     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 #57"></div>
  </section>
</div>
    <div class="postbit" id="30679" data-post-id="30679">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="smithaitufe" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  smithaitufe
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/imetallica" rel="nofollow">@imetallica</a> Thanks. I have started studying those topics now. GenServer is making sense now.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30679" 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-determine-contexts-with-phoenix-1-3/4367/59">Post #58</a>
	                </div>
	            </div>
              <div id="likers-container-30679" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30679"
                     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 #58"></div>
  </section>
</div>
    <div class="postbit" id="30680" data-post-id="30680">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="idi527" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  idi527
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>I don’t know if you can write a sample on how PubSub can be achieved.</p>
</blockquote>
<p>I’m not sure if that’s what you need, but have you checked <code>Registry</code> module in elixir? It has a note about how to use it as a local pub/sub system in the docs [0].</p>
<p>You can put it in a separate <code>app</code> under an umbrella and let processes from other apps subscribe to certain topics in it.</p>
<p><code>apps/pubsub/lib/pubsub.ex</code></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Test.PubSub do

  def start_link do
    Registry.start_link(:duplicate, __MODULE__)
  end

  def subscribe(topic) do
    Registry.register(__MODULE__, topic, [])
  end

  def unsubscribe(topic) do
    Registry.unregister(__MODULE__, topic)
  end

  def publish(topic, message) do
    Registry.dispatch(__MODULE__, topic, fn entries -&gt;
      for {pid, _value} &lt;- entries, do: send(pid, message)
    end)
  end
end
</code></pre>
<p><code>apps/pubsub/lib/pubsub/application.ex</code></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Test.PubSub.Application do
  @moduledoc false

  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      supervisor(Test.PubSub, [])
    ]

    opts = [strategy: :one_for_one, name: Test.PubSub.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
</code></pre>
<p>To subscribe for updates in <code>user</code> app in a phoenix channel you would do something like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Test.Web.UserChannel do
  use Test.Web, :channel

  def join("user:" &lt;&gt; name = topic, _params, socket) do
    {:ok, _owner_pid} = Test.PubSub.subscribe(topic) # subscribe to updates in "user:#{name}"
    {:ok, socket}
  end

  # and handle these updates
  def handle_info(:sacked, socket) do
    broadcast! socket, "sacked", %{}
    {:noreply, socket}
  end
end
</code></pre>
<p>Somewhere in the <code>user</code> app you would publish the message <code>:sacked</code> in case of a ban</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">...
Test.PubSub.publish("user:idiot", :sacked)
...
</code></pre>
<p>These are basically the examples from the <code>Registry</code> docs, and there are some more about other possible uses for it.</p>
<p>Note however that this pub/sub implementation is local. And if you are using phoenix, you might want to pick <code>Phoenix.PubSub</code> [1] instead which is not local, so that for the example above (with channels) you would probably use phoenix’s pub/sub library.</p>
<p>[0] <a href="https://hexdocs.pm/elixir/master/Registry.html#module-using-as-a-pubsub" rel="noopener nofollow ugc">https://hexdocs.pm/elixir/master/Registry.html#module-using-as-a-pubsub</a><br>
[1] <a href="https://hexdocs.pm/phoenix_pubsub/Phoenix.PubSub.html" class="inline-onebox" rel="noopener nofollow ugc">Phoenix.PubSub — Phoenix.PubSub v2.2.0</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="30680" 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-determine-contexts-with-phoenix-1-3/4367/60">Post #59</a>
	                </div>
	            </div>
              <div id="likers-container-30680" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30680"
                     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 #59"></div>
  </section>
</div>
    <div class="postbit" id="30701" data-post-id="30701">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Using Phoenix’s PubSub is better for this because it can broadcast messages between nodes, either using Erlang’s <code>pg</code> module, Redis or RabbitMQ. I think there’s a Kafka as well, but I’m not sure.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30701" 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-determine-contexts-with-phoenix-1-3/4367/61">Post #60</a>
	                </div>
	            </div>
              <div id="likers-container-30701" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30701"
                     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 #60"></div>
  </section>
</div>
    <div class="postbit" id="30719" data-post-id="30719">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/imetallica" rel="nofollow">@imetallica</a>, <a class="mention" href="/u/karmajunkie" rel="nofollow">@karmajunkie</a>, thanks for your replies. I guess I need to do more exercise on Elixir/Phoenix, including <code>PubSub</code>s. thanks again!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30719" 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-determine-contexts-with-phoenix-1-3/4367/62">Post #61</a>
	                </div>
	            </div>
              <div id="likers-container-30719" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30719"
                     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 #61"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/4367/load_more?page=7">Load more posts (16 remaining)</a>
</div></template></turbo-stream>