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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I do not use AMQP in elixir, but I do assume, that you have to either poll for messages one by one or register a callback or have to implement a behaviour to receive your messages.</p>
<p>Regardless how you receive your messages, in the code that is executed on receive you need to do your stuff.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="78309" 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/save-data-to-mongodb-through-code/13653/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-78309" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="78309"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="78310" data-post-id="78310">
  <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 want to connect to the database before my function start</p>
</blockquote>
<p>Then you can run this function as a <code>Task</code> after <code>supervisor(Repo, [])</code> in your application.<br>
Replace <a href="https://github.com/dersar00/friends/blob/master/lib/friends/application.ex" rel="noopener nofollow ugc">https://github.com/dersar00/friends/blob/master/lib/friends/application.ex</a> with</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Friends.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    import Supervisor.Spec
    children = [
      supervisor(Friends.Repo, []),
      worker(Task, [&amp;Friends.save/0], restart: :transient)
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Friends.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
</code></pre>
<p>Don’t forget to delete <a href="https://github.com/dersar00/friends/blob/master/lib/friends.ex#L10" rel="noopener nofollow ugc">https://github.com/dersar00/friends/blob/master/lib/friends.ex#L10</a></p>
<blockquote>
<p>My architecture is:<br>
I build my project using a microservices architecture(rabbitmq), one of my worker need to be builded using elixir, when I get message from my queue, for example I want to save it to db, so how do it?</p>
</blockquote>
<p>You would call your function like <code>Friends.save(data)</code> from a handler which handles new messages from rabbitmq, just like a phoenix controller which handles new requests.</p>
<p>You wouldn’t need the <code>worker(Task, [&amp;Friends.save/0], restart: :transient)</code> from above.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="78310" 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/save-data-to-mongodb-through-code/13653/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-78310" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="78310"
                     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>