phlex-reactive

Reactive Phlex components for Rails — Livewire-style actions and live cross-tab updates, without writing Stimulus controllers or hand-picking Turbo Stream targets.

Get started View on GitHub


class Counter < ApplicationComponent
  include Phlex::Reactive::Streamable
  include Phlex::Reactive::Component

  reactive_state :count
  action :increment
  action :decrement

  def initialize(count: 0) = @count = count
  def id = "counter"

  def increment = @count += 1
  def decrement = @count -= 1

  def view_template
    div(id:, **reactive_attrs) do
      button(**on(:decrement)) { "−" }
      span { @count }
      button(**on(:increment)) { "+" }
    end
  end
end

That’s the whole counter. No Stimulus controller. No .turbo_stream.erb. No route. No hand-picked target.

Why phlex-reactive

  • Actions are Ruby methods. Declare action :increment; the client calls it.
  • Re-render is auto-targeted. A component owns a stable id; the response replaces it. You never pick a target.
  • One unit for clicks AND broadcasts. The same component re-renders for a local action and a server-pushed live update.
  • State lives in your database, behind a signed identity — no attacker-controlled snapshot.
  • One tiny client runtime. A single generic Stimulus controller, registered once, drives every reactive component.

Pair it with pgbus for transactional, reconnect-safe live updates over Postgres — no Action Cable, no Redis.

Documentation

Examples


This site uses Just the Docs, a documentation theme for Jekyll.