sanswork

sanswork

Automatic channel messages

Hi,

I’m trying to send a message automatically to all users subscribed to a specific channel every couple of seconds(it’s a status update type message). I’ve gotten everything working with a handle_info which I can test by setting up a :timer.send_interval(5_000, :updates) in the join function of the channel module. That works fine but with each user that joins another timer starts so its not ideal for actual usage. Any idea how to start a timer like that outside of the channel?

I’ve tried creating a function in the channel called start_updates which just has that timer line in it then calling it from lib/myapp.ex in the start function with Myapp.UpdateChannel.start_updates() and though it does get called the timer inside doesn’t appear to work.

Most Liked

sanswork

sanswork

Thanks for the suggestion NobbZ

Someone on Slack linked http://stackoverflow.com/questions/32085258/how-to-run-some-code-every-few-hours-in-phoenix-framework from which I got the following solution using a GenServer

I created a new file in /lib/myapp/update_users.ex

defmodule MyApp.UpdateUsers do
  use GenServer
  import Ecto
  import Ecto.Query, only: [from: 1, from: 2]

  def start_link do
    GenServer.start_link(__MODULE__, %{})
  end

  def init(state) do
    Process.send_after(self(), :work, 5_000) # In 5 seconds
    {:ok, state}
  end

  def handle_info(:work, state) do
    #get data here.  I also set the start_id from the state or set it to 0

    MyApp.Endpoint.broadcast("updates:activities", "activities", %{source: a.source, points: a.points, name: a.name})

    # Start the timer again
    Process.send_after(self(), :work, 5_000) # In 5 seconds

    #pass the max_id into the state so we can skip previously seen data in next iteration
    {:noreply, %{"last_id" => max_id}}
  end
end

Then added

worker(MyApp.UpdateUsers,[])

into lib/myapp.ex in the start/2 method and it seems to work a charm

NobbZ

NobbZ

I’m not quite sure if it really helps you, but there is quantum available at hex.pm, perhaps it may help you.

A quick and dirty solution which comes to my mind using it, were to have a function which connects to the channel, sends the message and leaves the channel again.

A much saner way, were to start a GenServer (or similar) with your app, which does connect to the channel and gets a message from quantum and on this message it gets translated into the channel message and then send. But I’m not quite sure, how GenServer-messages and Channel-messages behave alltogether.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New

We're in Beta

About us Mission Statement