Radio server with Plug & Cowboy

I want to implement a simple radio server that stream to all connected users (the client is the browser) a playlist of songs

so far I managed to stream audio file to the browser

defmodule Plugtest do
  import Plug.Conn

  @chunk_size 128
  @song_path "./song.mp3"
  @content_type "audio/mpeg"
  @status_code 200

  def init(opts), do: opts

  def call(conn, opts) do
    conn = conn
    |> send_chunked(@status_code)
    |> put_resp_content_type(@content_type)

    File.stream!(@song_path, [], @chunk_size)
    |> Enum.into(conn)
  end
end

IO.puts "streaming @ http://localhost:4000"
Plug.Adapters.Cowboy.http Plugtest, []

I run it via $:iex -S mix

How do I stream it so all connected users will hear it from the same second and not from the start ?

2 Likes

That’s a lot more complicated, but there’s a protocol specific for that purpose: RTSP. I’ve never looked at the specs, but you probably want to do something similar

1 Like