Skip to content

rage-rb/datastar-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rage + Datastar Example

This project demonstrates how to integrate the Rage framework with Datastar using the official Ruby SDK.

Setup

bundle install
rage s

Visit http://localhost:3000.

Datastar Integration

The integration consists of two files:

config/initializers/datastar.rb

Configures the Datastar SDK for Rage:

  • Finalizer: Sets a custom finalizer that stores the response on the view context instead of rendering immediately. This is necessary because the SDK allows multiple datastream.stream calls per action, and each call triggers the finalizer. Rendering directly would cause a "Render was called multiple times" error.

  • Executor: Defines a custom executor that uses Fibers instead of threads, aligning with Rage's fiber-based concurrency model.

app/controllers/concerns/datastar_integration.rb

A controller concern that:

  • Exposes a datastar helper to access the Datastar object.
  • Provides a datastar_response= writer used by the finalizer.
  • Defines an around_action that waits for the action to complete, then renders the accumulated SSE response using Rage's render sse:.

About

Rage integration with Datastar using the official Ruby SDK

Resources

Stars

Watchers

Forks

Contributors