Delaying message broadcast

Hey guys,

I have an API where i create some publications and broadcast a message for each publication that i created.

The issue is: When i create a publication, that resource will be available for users after one minute, but at this point, message is already broadcasted and user will try to acess it and gets an error.

My doubt: Is that a way to delay this message broadcast, without blocking the request sended to create a publication?

The best approach would be to publish the broadcast when the resource is available, after that one minute. What triggers the resource becoming available? Wherever that codepath is, is where you can hook in the broadcast. If it’s third-party processing, see if they support a post-back URL to your app, which you can then use to broadcast. Make sense?

4 Likes
Task.start(fn ->
  :timer.sleep(60*1000)
  PhoenixApp.Web.Endpoint.broadcast("channel_name", "xx_update", %{payload: %{id: 1} })
end)

but this is very hackish - you really want to broadcast directly when the publication is ready… (as @chrismccord said…)

1 Like

Hey @chrismccord thanks for you time!

Your approach it’s the best way to solve the problem, but unfortunately, it’s a third party service that can’t provide me a callback or something like that.
Actually I use the approach that @outlog shared here, but with this kind of solution, whole process still blocking until the sleeps time end. :frowning:

??? The process which calls Task.start does not block, and I don’t see how the blocking inside of the task could possibly matter, since it’s not visible to anything.

2 Likes

@sribe My bad! We are using :timer_sleep in a very very bad way here :frowning:

We are using it without a Task.start, so, that’s why we getting a blocking(long time waits) to return something trough publication creation API calls :frowning:

I’ll adjust the code to fit the example and probably this will solve our issue :slight_smile: