Phoenix.Sync.Controller (Phoenix.Sync v0.3.4)
View SourceProvides controller-level integration with sync streams.
Unlike Phoenix.Sync.Router.sync/2
, which only permits static shape
definitions, in a controller you can use request and session information to
filter your data.
Phoenix Example
defmodule MyAppWeb.TodoController do
use Phoenix.Controller, formats: [:html, :json]
import Elixir.Phoenix.Sync.Controller
alias MyApp.Todos
def all(conn, %{"user_id" => user_id} = params) do
sync_render(
conn,
params,
from(t in Todos.Todo, where: t.owner_id == ^user_id)
)
end
end
Plug Example
You should use Elixir.Phoenix.Sync.Controller
in your Plug.Router
, then within your route
you can use the sync_render/2
function.
defmodule MyPlugApp.Router do
use Plug.Router, copy_opts_to_assign: :options
use Elixir.Phoenix.Sync.Controller
plug :match
plug :dispatch
get "/todos" do
sync_render(conn, MyPlugApp.Todos.Todo)
end
end
Summary
Functions
@spec sync_render(Plug.Conn.t(), Plug.Conn.params(), Electric.Shapes.Api.shape_opts()) :: Plug.Conn.t()
Return the sync events for the given shape as a Plug.Conn
response.