Live Update
========

Live Update provides a generic live-update layer for Drupal. It is intentionally
not tied to chat: any module can publish events to named channels, and browser
code can subscribe to those channels to update chat messages, blocks, views or
custom rendered fragments.

The module works without an external realtime service by using periodic
JavaScript polling against Drupal. This keeps it useful on shared hosting. That
polling transport is not WebSocket or Server-Sent Events push delivery; push
transports such as a Go, Node.js, WebSocket or SSE gateway can be added later by
companion modules that implement the Live Update API.

Live Update complements, rather than replaces, other realtime projects. Chatroom
can use Live Update as one delivery provider. Node.js integration remains a
separate Socket.IO gateway for sites that want to run a Node.js server. Live
content is a separate WebSocket/Ratchet approach focused on live content
updates. Live Update's base module is deliberately simpler: it provides a
Drupal-side polling/event API and block refreshes without requiring a long-lived
server process.

Setup
-----

1. Enable Live Update:

       vendor/bin/drush en liveupdate -y
       vendor/bin/drush updb -y
       vendor/bin/drush cr

2. Configure the polling interval at `/admin/config/services/liveupdate`.

3. Enable live updates on individual blocks, enable an integration module such
   as Chatroom, or publish custom events with `liveupdate_publish()`.

Blocks
------

Go to `/admin/structure/block`, place or configure a block, and open the
`Live Update` section. Enable `Update this block live`.

When the block receives a Live Update event, browsers refresh that block without
reloading the whole page. The default update mode replaces the whole block so
the refreshed markup keeps titles, wrappers, contextual links and attributes in
sync.

Live Update stores block options as third-party block configuration, so they are
exportable with normal Drupal configuration.

Block refreshes use Drupal core's HTMX integration internally. Site builders do
not need to configure HTMX directly.

Remote JSON blocks
------------------

Enable the `liveupdate_remote` submodule to create live-updating table blocks
from remote JSON APIs. It is useful for currencies, crypto prices, stock quotes,
weather, status pages and public data APIs. Drupal fetches and caches the JSON
server-side, then refreshes the placed block. Remote URLs must use public HTTP
or HTTPS hosts. Localhost, private IP ranges, reserved IP ranges and oversized
responses are blocked to reduce SSRF and provider-abuse risk. Redirects are
followed only when each target also passes the same public URL validation.

Chatroom integration
--------------------

When Chatroom is installed, Live Update appears as a message delivery method at
`/admin/config/user-interface/chatroom`. Enable it alongside JavaScript polling
for fallback. Chatroom messages are published to channels named
`chatroom_{cid}` and Live Update asks Chatroom to verify room access before
returning events.

Generic browser updates
-----------------------

Elements can subscribe to a channel with a data attribute:

    <div data-liveupdate-channel="example:dashboard" data-liveupdate-mode="replace">
      ...
    </div>

When an event payload contains `html` or `rendered`, the Live Update client
updates matching elements. `data-liveupdate-mode="replace"` replaces the whole
element; otherwise only its inner HTML is replaced. If the payload contains
`refreshPath`, the client requests fresh markup from Drupal first. Elements may
also provide `data-hx-get` and `data-hx-trigger="liveupdate:refresh"` to let
Drupal core's HTMX integration perform the fragment request and swap.

API
---

Publish an event:

    liveupdate_publish('example:dashboard', 'dashboard.updated', [
      'html' => $rendered_markup,
    ]);

Modules that expose private channels must implement
`hook_liveupdate_channel_access()`. Channels without an access implementation are
denied by default.

Push transports can implement `hook_liveupdate_publish()` to forward the same
event to an external service.

Payload safety
--------------

Live Update treats event payloads as data supplied by the publishing module. If a
payload contains `html` or `rendered`, the browser client inserts that markup
into subscribed elements. Publishing modules must therefore only send trusted,
already-sanitized markup, preferably generated by Drupal render arrays. For
untrusted content, publish data and let the receiving code render it safely, or
publish a `refreshPath` to a Drupal route that performs normal access checks and
escaping.

Related projects
----------------

* Node.js integration provides a realtime API backed by a separate Node.js
  Socket.IO server application that maintains open browser connections. It is a
  good fit for sites already able to operate that server. Live Update's built-in
  transport does not require Node.js and uses Drupal polling instead.
* Live content provides live updates through a WebSocket service using Ratchet
  and a Drush-run server process. Live Update's base module does not run a
  WebSocket server; it provides a smaller Drupal polling/event API that can be
  extended by future push transports.
* LiveReload-style tools are primarily development workflow helpers that refresh
  browsers when source files change. Live Update is for runtime application
  events such as block refreshes, chat integrations, dashboards and rendered
  fragments.
