rogerweb

rogerweb

Best way to push messages from SQS to user connected via websocket

Hi,

In AWS SQS, I have one FIFO queue for each user with messages coming from another system. When a user joins his own Phoenix channel (e.g. user:42), I start a Broadway process that keeps pulling messages from user’s queue in SQS and pushing them to the user via websocket.

My current implementation calls Broadway.start_link/2 from the join/3 callback of Phoenix Channel, like this:

Broadway.start_link(__MODULE__,
  name: String.to_atom("Broadway-" <> user_id),
  producer: [
    module: {BroadwaySQS.Producer,
      queue_url: queue_url(user_id)
    }
  ],
  processors: [
    default: [
      concurrency: 1
    ]
  ],
  batchers: [
    default: [
      batch_size: 10,
      batch_timeout: 1000
    ]
  ]
)

The questions are:

  1. Is this right in terms of processes and supervision?

  2. When the user disconnects, I see that all his Broadway-related processes are gone, which is good, but I’m wondering if this is a proper stop/shutdown or if I should implement something.

Best regards,

Roger

Most Liked

trisolaran

trisolaran

If you want Broadway to properly drain messages and perform a graceful shutdown, it should be started as a child of a supervision tree. See the bullet point “Graceful shutdown” here. Typically, people start Broadway as a child of the supervision tree of their main application.

Starting a new Broadway pipeline whenever a user joins a channel seems like overkill to me, as it is using a separate SQS queue for each user (do you create them dynamically?). Without knowing the exact problem you’re trying to solve, I would probably attempt to have a constant number of queues (1 is possible) shared by all users and a single pipeline that fetches messages and routes them to the destination channel.

rogerweb

rogerweb

Thanks for your inputs.

it should be started as a child of a supervision tree

It is under the Phoenix supervision tree, but I’m not sure it means Broadway will perform a graceful shutdown.

Typically, people start Broadway as a child of the supervision tree of their main application.

That’s exactly how I usually do too, but only if I know which queue I’ll be reading from a priori, which is not the case.

do you create them dynamically? [a queue per user]

Yes, that other external system creates the queue as part of the provisioning process of a new user.

I would probably attempt to have a constant number of queues (1 is possible) shared by all users and a single pipeline that fetches messages and routes them to the destination channel.

The problem is the destination channel (the user) might not be there to receive the fetched message, so we would have to keep his message(s) in the queue for certain time, then after some short time they would appear again but the destination might still be offline and so on and on. Imagine we have hundreds of offline users. It seems odd to keep fetching messages that we can’t deliver. Also, online users might be penalized (in terms of delay) as their messages might be “buried” under a bunch of messages targeted to offline users.

The problem I’m trying to solve is: how to deliver user-specific enqueued messages to the users when they get online. Messages might be enqueued at any time, but dequeued only when the user is online.

Again, thanks a lot for you time and let me know if I can be more exact or specific.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement