Elixir how to send data to websockets repeatedly

I have a process that reads a file, for this test it reads a line every second

I want all connected websockets to receive the same line

e.g;

if user 1 connects he receives line 1, after a second line 2… after a second line 3… line 4… and so on

if user 2 connects after 3 seconds his first line that he will receive will be the 3rd line of the file, a second after he and receives line 4 and so on…

how do I implement this ? should I use erlang’s library cowboy v1 or v2 ?

this is the code for the process that read from file using PubSub

defp stream_from_file(fpath, bytes) do
    File.stream!(fpath, [], bytes)
    |> Enum.each(fn chunk ->
      PubSub.publish(:topic1, {:topic1_data, chunk})
      :timer.sleep(1_000)
    end)
    stream_from_file(fpath, bytes)
  end
2 Likes

Well using Phoenix.PubSub that just involves broadcasting to the topic that they are all joined to.

1 Like

using {:pubsub, “~> 0.0.2”} not Phoenix