Hello, I have a code organization question.
Let’s say I have a Blog
app with posts.ex
context which is responsible for managing posts. When a new post is added - I’d like to send a pubsub message about it using BlogWeb.Endpoint.broadcast
.
What would be the right way to do it? The simplest one to me is to broadcast inside Blog.create_post
, but it feels that Blog
shouldn’t depend on BlogWeb
. Other options I see are:
- Add another
posts.ex
context insideBlogWeb
, that will either wrap methods likecreate_post
(or for claritycreate_post_and_broadcast
and add broadcast there. This feels not to so bad, but caller has to know that for some operations he has to go toBlogWeb.Posts
instead ofBlog.Posts
. - Or again create this new
posts.ex
and just provide broadcast methods there, then caller must call broadcast aftercreate_post
, which sounds error prone.
Could you please tell if there’s a clear idiomatic way of doing it or share a good example? Thanks!