Datastar SSE helpers for Elixir/Phoenix.
A trivially small library: SSE connection management, signal patching, DOM element patching. That's it.
Quick Example
def increment(conn, _params) do
signals = Dstar.read_signals(conn)
count = signals["count"] || 0
conn
|> Dstar.start()
|> Dstar.patch_signals(%{count: count + 1})
endModules
Dstar.SSE— Open SSE connections, send raw eventsDstar.Signals— Read signals from requests, patch signals on the clientDstar.Elements— Patch and remove DOM elementsDstar.Actions— Generate@post(...)expressions for Datastar attributesDstar.Plugs.Dispatch— Optional dynamic event dispatch plug
Summary
Functions
Checks if an SSE connection is still open.
Logs a message to the browser console via SSE.
Generates a @delete(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
Executes JavaScript on the client by appending a script tag via SSE.
Generates a @get(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
Generates a @patch(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
Patches a DOM element on the client via SSE.
Patches signals on the client via SSE.
Generates a @post(...) expression for Datastar attributes.
Generates a @put(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
Reads Datastar signals from the request.
Redirects the client to the given URL via JavaScript.
Removes DOM elements on the client via SSE.
Removes signals from the client by setting them to nil.
Starts an SSE connection on the given Plug conn.
Starts an SSE stream with per-tab deduplication.
Functions
Checks if an SSE connection is still open.
Returns {:ok, conn} if open, {:error, conn} if closed.
Useful in streaming loops to detect disconnections.
Example
case Dstar.check_connection(conn) do
{:ok, conn} -> stream_loop(conn)
{:error, _} -> :ok
end
Logs a message to the browser console via SSE.
Example
conn |> Dstar.console_log("Debug info")
Generates a @delete(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
Executes JavaScript on the client by appending a script tag via SSE.
Example
conn |> Dstar.execute_script("alert('Hello!')")
Generates a @get(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
See Dstar.Actions.get/3.
Generates a @patch(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
Patches a DOM element on the client via SSE.
Requires a :selector option.
Example
conn |> Dstar.patch_elements("<span id=\"count\">42</span>", selector: "#count")
Patches signals on the client via SSE.
Example
conn |> Dstar.patch_signals(%{count: 42})
Generates a @post(...) expression for Datastar attributes.
Examples
Dstar.post(MyAppWeb.CounterHandler, "increment")
# => "@post('/ds/my_app_web-counter_handler/increment', {headers: ...})"
Dstar.post("increment")
# => "@post('/ds/' + $_dstar_module + '/increment', {headers: ...})"
See Dstar.Actions.post/3.
Generates a @put(...) expression for Datastar attributes.
See Dstar.post/2 for usage — same API, different HTTP verb.
See Dstar.Actions.put/3.
Reads Datastar signals from the request.
For GET requests, reads from query params. For POST/PUT/etc, reads from the JSON body.
Example
signals = Dstar.read_signals(conn)
count = signals["count"] || 0
Redirects the client to the given URL via JavaScript.
Example
conn |> Dstar.redirect("/workspaces")
Removes DOM elements on the client via SSE.
Example
conn |> Dstar.remove_elements("#old-item")
Removes signals from the client by setting them to nil.
Accepts a single path string or list of dot-notated paths.
Examples
conn |> Dstar.remove_signals("user.profile.theme")
conn |> Dstar.remove_signals(["user.name", "user.email"])
Starts an SSE connection on the given Plug conn.
Sets content type to text/event-stream, disables caching,
and initiates a chunked response. Returns the conn.
Example
conn = Dstar.start(conn)
Starts an SSE stream with per-tab deduplication.
Requires Dstar.Utility.StreamRegistry in your supervision tree
and a tabId signal in your root layout. See
Dstar.Utility.StreamRegistry module docs for setup.
Example
conn = Dstar.start_stream(conn, current_user.id)