Realtime comment updates
Realtime comment updates carry a saved comment from Rails to every open comments view. A database commit queues a relay job. The job publishes the comment on the comments Action Cable stream, and client integrations add the message to their local view without waiting for a page refresh. See Comment experience implementations for the parent overview.
The realtime path sits beside the Rails comment backend, which owns persistence and HTTP responses. Its consumers are described in the React comments user interface, Turbo and Stimulus comments UI, and ReScript comment demo pages.
Update relay
The update relay is the server-side path from a successful comment change to a broadcast. Comment schedules CommentRelayJob after each committed save, so the request that changes the record does not need to manage each connected browser. The job broadcasts only while the record still exists. This keeps a deleted record from being sent as a new update. References: app/…/comment.rb• L3-5, app/…/comment_relay_job.rb• L3-6
When a comment is created or updated, the relay sends the comment object as the message payload. The same callback also runs for a destroy commit, but the job's destroyed-record check suppresses that message. As a result, this realtime path adds new or changed comments; it does not tell clients to remove an existing comment after deletion. References: app/…/comment.rb• L3-5, app/…/comment_relay_job.rb• L3-6, app/…/comments_controller.rb• L49-68
Action Cable channel
The Action Cable channel is the server-side subscription boundary for comment messages. CommentsChannel accepts a subscription and attaches it to the shared comments stream. The Rails route exposes the Action Cable server at /cable, which gives browser consumers a WebSocket endpoint. References: app/…/comments_channel.rb• L3-6, config/routes.rb• L21-27
The stream name is the contract between the relay and all subscribers. The relay broadcasts to comments, while the channel streams from comments; the channel name used by clients is CommentsChannel. A mismatch in either name prevents delivery even when the comment save succeeds. References: app/…/comment_relay_job.rb• L3-6, app/…/comments_channel.rb• L3-6, client/…/comments_controller.js• L48-65, client/…/CommentBox.jsx• L56-79
This diagram shows the path from a committed comment change to the browser views.
flowchart LR
A[Comment commit] --> B[CommentRelayJob]
B --> C[Action Cable broadcast: comments]
C --> D[CommentsChannel subscription]
D --> E[Stimulus comments controller]
D --> F[React CommentBox]
D --> G[ReScript comment demo]
E --> H[Insert comment into HTML list]
F --> I[Dispatch message received]
G --> J[Update ReScript state]
Client consumers
The client integrations use the same channel contract but update their own view state. Each consumer creates an Action Cable client from the URL in the page metadata, subscribes to CommentsChannel, and handles incoming comment data. The shared URL helper keeps the endpoint supplied by Rails rather than hard-coding it in each UI. References: client/…/actionCableUrl.js• L1-4, app/views/layouts/application.html.erb#L21-L29, client/…/comments_controller.js• L48-65, client/…/CommentBox.jsx• L56-79
Stimulus consumer
The Stimulus consumer serves the server-rendered comments page. On connection it opens a cable consumer and subscribes to CommentsChannel; on receipt it converts the comment text from Markdown to HTML and inserts the new block at the start of the list. It unsubscribes and closes the cable when the controller leaves the page. References: client/…/comments_controller.js• L11-12, client/…/comments_controller.js• L48-71, app/views/comments/stimulus.html.erb#L5-L19
React consumer
The React consumer serves the Redux-backed comment interface. It fetches the current list when the component mounts, then subscribes for later changes. Each cable message becomes a MESSAGE_RECEIVED action, and the reducer adds it only when its id is not already present. This id check prevents the HTTP submit response and the cable broadcast from showing the same comment twice. References: client/…/CommentBox.jsx• L56-91, client/…/commentsActionCreators.js• L30-35, client/…/commentsReducer.js• L36-45
ReScript consumer
The ReScript demo uses a small typed bridge over the browser Action Cable object. It subscribes when the component mounts and removes the subscription during cleanup. A received comment becomes the single item in a new comment state, while the initial list comes from the Rails JSON endpoint. References: client/app/bundles/comments/rescript/bindings/ActionCable.res#L1-L38, client/app/bundles/comments/rescript/ReScriptShow.res#L31-L69, client/app/bundles/comments/rescript/Actions/Actions.res#L27-L57
Connection configuration
The page supplies the cable URL through action_cable_meta_tag, and the JavaScript helper reads that value before creating a consumer. In production, PRODUCTION_HOST sets the secure wss endpoint and limits allowed request origins to the matching HTTPS host. A missing or incorrect page URL prevents the client from reaching the channel even though the Rails route remains available. References: app/views/layouts/application.html.erb#L17-L29, client/…/actionCableUrl.js• L1-4, config/…/production.rb• L95-100
The application includes Action Cable in the client asset setup so the browser integrations can use the same cable client. The server endpoint and the client library are separate parts of the contract: both must be available for an update to reach a view. References: config/routes.rb• L8-9, config/routes.rb• L26-27, config/…/clientWebpackConfig.js• L10-19
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the relay stream name, channel stream name, and client channel name aligned:
commentsis the stream andCommentsChannelis the channel. - Preserve the comment payload fields expected by the clients, especially
id,author, andtext; the HTTP and cable paths should remain compatible. - If the cable endpoint or host changes, update the Rails page metadata and the production origin rules as one contract.
- If deletion must appear in open browsers, add a defined client event and removal rule; the current job suppresses destroyed records.
- Keep the React duplicate guard aligned with the identity field used in broadcast and submit responses.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The relay and channel use different stream names. | Subscribers connect but receive no comment messages. | Treat comments as the shared stream contract. |
A client uses a different channel name from CommentsChannel. | The browser does not subscribe to the comment channel. | Use the channel class name in each consumer. |
| The page has no usable Action Cable metadata URL. | The client cannot open the cable connection. | Render the cable meta tag and read it through the shared URL helper. |
| A comment is deleted after the commit callback queues the job. | The job sends no deletion event, so an open client can keep the old item. | Use a refresh or add an explicit deletion protocol if removal is required. |
| The React HTTP response and cable message arrive for the same comment. | The comment can appear twice without id-based de-duplication. | Keep the MESSAGE_RECEIVED id check in the React state path. |
Key files
| File | Why to read it |
|---|---|
| app/…/comment.rb | Defines when a committed comment enters the relay path. |
| app/…/comment_relay_job.rb | Defines the broadcast boundary and the destroyed-record rule. |
| app/…/comments_channel.rb | Defines the server-side subscription to the shared stream. |
| config/routes.rb | Exposes the /cable Action Cable endpoint. |
app/views/layouts/application.html.erb | Supplies the cable URL metadata to browser pages. |
| client/…/actionCableUrl.js | Provides the common client lookup for that URL. |
| client/…/CommentBox.jsx | Shows the React subscription and handoff into application state. |
| client/…/commentsReducer.js | Shows how React stores incoming messages and avoids duplicates. |
| client/…/comments_controller.js | Shows the Stimulus subscription and direct HTML update path. |
client/app/bundles/comments/rescript/ReScriptShow.res | Shows the ReScript subscription and state update path. |
client/app/bundles/comments/rescript/bindings/ActionCable.res | Defines the typed bridge used by the ReScript consumer. |